简体   繁体   中英

Showing softkeyboard on change in focus edittext android

I am using the follwing code to show a keyboard of an edit box when the focus is changed.

  no1.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
            if(keyCode == 66) {

                no2.requestFocus();

            }
            }
            return false;
        }
    });


    no2.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
            if(keyCode == 66) {
                 //Log.e("I am inside no2","no2");
                no3.requestFocus();

                return true;


            }
            }
            return false;
        }
    });



    no2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
               Log.e("In","Innnnnn");
               InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(no2, InputMethodManager.SHOW_IMPLICIT);         
            }
        }
    });

However the keyboard does not show. The log statement is being printed. So that means it is entering the focusChanged method. Any ideas what I am doing wrong?

使用InputMethodManager控制键盘。

Try this...

final AlertDialog dialog = ...;

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

try this

InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM