简体   繁体   中英

Android : show keyboard in edittext

Am stuck with a pretty simple issue in the Android App am coding. I have a list of EditText objects, one in each row.

When the user long presses the EditText , I need to show the keyboard. When the user does a long press, then I call this method :

private void setNameAsEditable(View rowView, boolean setToEditable) {

    EditText textView = (EditText) rowView
        .findViewById(R.id.edittext_name);
    textView.setFocusableInTouchMode(setToEditable);
    textView.setFocusable(setToEditable);
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
}

The edittext becomes editable (the underline and the cursor appears, as you can see below)

在此输入图像描述

but the keyboard does not come up.

I tried various solutions from stackoverflow (like this ), but in vain.

I even tried

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

I want the soft-keyboard to come up as soon as the user long-presses the EditText . Can someone please help?

Try it, for me it works fine.

 et =(EditText) findViewById(R.id.edittext_name);
      imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
      et.setText("hide key board");
      et.setFocusable(false);
     et.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            et.setFocusableInTouchMode(true);
            imm.showSoftInput(et, 0);
            et.setText("show key board long pressed");
            return false;
        }
    });

This is what worked finally ( Amit's answer + Tamilan's answer) Thanks so much!

private void setNameAsEditable (View rowView, boolean setToEditable) {

    EditText textView = (EditText) rowView
            .findViewById(R.id.edittext_name);
    textView.setFocusableInTouchMode(setToEditable);
    textView.setFocusable(setToEditable);

    InputMethodManager imm = (InputMethodManager) getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);

    if (setToEditable) {
         textView.requestFocus();
        imm.showSoftInput(textView, 0);
    }
}
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

检查xml布局中是否有android:focusable =“false”或android:focusableInTouchMode =“false”。

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