Nice programing

android : Done 키를 누르면 소프트 키보드가 작업을 수행합니다.

nicepro 2020. 11. 9. 20:58
반응형

android : Done 키를 누르면 소프트 키보드가 작업을 수행합니다.


EditText가 있습니다. 텍스트를 입력 한 후 사용자 Done가 소프트 키보드 키를 누르면 버튼 클릭 이벤트에서도 구현 한 일부 검색 작업을 직접 수행해야합니다.


이 시도

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});

이 시도

DONERETURN 모두에 대해 작동합니다 .

EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                            || actionId == EditorInfo.IME_ACTION_DONE) {
                        // Do your action
                        return true;
                    }
                    return false;
                }
            });

를 잡아서 KeyEvent키 코드를 확인합니다. FLAG_EDITOR_ACTIONEnter 키가 "next"또는 "done"으로 자동 레이블 지정된 IME에서 오는 Enter 키를 식별하는 데 사용됩니다.

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

여기 에서 문서를 찾으 십시오 .

두 번째 방법

myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    int result = actionId & EditorInfo.IME_MASK_ACTION;
    switch(result) {
    case EditorInfo.IME_ACTION_DONE:
        // done stuff
        break;
    case EditorInfo.IME_ACTION_NEXT:
        // next stuff
        break;
    }
 }
});

이 시도

이것은 키보드에 Enter 기호 또는 다음 화살표 기호가 표시되는지 여부에 관계없이 두 조건에서 작동합니다.

YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener()
    {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT)
            {
                //Perform Action here 
            }
            return false;
        }
    });

if u r facing red line then do this... import Keyevent and EditorInfo by pressing alt+enter then all the errors remove it will properly.......

참고URL : https://stackoverflow.com/questions/7584036/android-softkeyboard-perform-action-when-done-key-is-pressed

반응형