Android : show keyboard in edittext
我在Android App am编码中遇到了一个非常简单的问题。 我有一个
当用户长按
1 2 3 4 5 6 7 8 9 | 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); } |
edittext变得可编辑(下划线和光标出现,如下所示)
但键盘没有出现。
我尝试了stackoverflow的各种解决方案(像这样),但是徒劳无功。
我甚至试过了
1 | this.getWindow().setSoftInputMode ( WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); |
一旦用户长按
这是最终奏效的(Amit的回答+ Tamilan的答案)非常感谢!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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); } } |
试试吧,对我来说它运作正常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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; } }); |
1 2 3 | 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"。