简体   繁体   中英

keyboard not been shown for edittext

I have a select box and edit text in a view.If you perform a longClick on customNumberPicker.It will hide the customNumberPicker and display the Edittext.(The same thing is done for edit text)It is working fine.

But I would like also that the keyboard will be open when switching to the edit text mode.

Here is my code

final NumberPicker numberPicker = (NumberPicker) dialog.findViewById(R.id.npWeight);
    final EditText editText = (EditText) dialog.findViewById(R.id.edWeight);

    numberPicker.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            if (numberPicker.getVisibility() == View.VISIBLE) {
                editText.setVisibility(View.VISIBLE);
                numberPicker.setVisibility(View.GONE);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                showKeyboard(MyApplication.mainActivity);
            }

            return true;
        }
    });

    editText.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
                editText.setVisibility(View.GONE);
                numberPicker.setVisibility(View.VISIBLE);
         //       hideKeyboard(MyApplication.mainActivity);
            return true;
        }
    });


private static void showKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert methodManager != null && view != null;
        methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }

Try to show keyboard with below options:

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);                   

inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(),InputMethodManager.SHOW_FORCED, 0);

or

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

For fragment try below code :

public void showKeybarod(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if(getActivity() != null && !getActivity().isFinishing() && getUserVisibleHint()) {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    },300);
}

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