简体   繁体   English

Android:在edittext中显示键盘

[英]Android : show keyboard in edittext

Am stuck with a pretty simple issue in the Android App am coding. 我在Android App am编码中遇到了一个非常简单的问题。 I have a list of EditText objects, one in each row. 我有一个EditText对象列表,每行一个。

When the user long presses the EditText , I need to show the keyboard. 当用户长按EditText ,我需要显示键盘。 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) edittext变得可编辑(下划线和光标出现,如下所示)

在此输入图像描述

but the keyboard does not come up. 但键盘没有出现。

I tried various solutions from stackoverflow (like this ), but in vain. 我尝试了stackoverflow的各种解决方案(像这样 ),但是徒劳无功。

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 . 一旦用户长按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! 这是最终奏效的(Amit的回答+ Tamilan的答案)非常感谢!

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”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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