Android:强制键盘显示并聚焦于编辑文本
提前谢谢你的帮助 我希望在下面代码的末尾或执行过程中出现一个键盘Android:强制键盘显示并聚焦于编辑文本,android,android-layout,Android,Android Layout,提前谢谢你的帮助 我希望在下面代码的末尾或执行过程中出现一个键盘 // edit text EditText editText = new EditText(activity); editText.setBackgroundColor(Color.WHITE); editText.setGravity(Gravity.TOP); editText.setText("Type here..."); RelativeLayout relativeLa
// edit text
EditText editText = new EditText(activity);
editText.setBackgroundColor(Color.WHITE);
editText.setGravity(Gravity.TOP);
editText.setText("Type here...");
RelativeLayout relativeLayoutEditText = new RelativeLayout(activity);
RelativeLayout.LayoutParams paramRelativeLayoutEditText = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 43 * display.getHeight() / 80 );
paramRelativeLayoutEditText.addRule(RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutEditText.addView(editText,paramRelativeLayoutEditText);
// add all created views to rootView
rootView.addView(relativeLayoutEditText, paramEditText);
// open keyboard
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
但键盘只有在触摸editText字段(即用手指)后才会出现。有没有一种方法,我可以使董事会自动出现,而无需使用物理触摸
顺便说一句,我知道如何指定宽度和高度并不完全是正确的做法。Add
editText.requestFocus();
试试这个:
EditText editText = (EditText ) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
确保编辑文本在触摸模式下可聚焦。你可以用两种方法
在xml中:
android:focusableInTouchMode="true"
在Java中:
editText.setFocusableInTouchMode(true);
试试这个
EditText textView = (EditText ) findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
可能。上面的许多内容也来源于此链接
试试这个:
EditText textView = (EditText )findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
多亏了@Shubham的链接,我才明白这一点。然而,解决方案并不是链接中给出的答案。这是第二个答案
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
编辑:
一旦使用上述解决方案,键盘将保持在屏幕上,直到用户按下后退按钮或主页按钮(有时需要几次)。要卸下键盘,请使用以下命令
imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);
在我的例子中,rootView是当前活动的rootView。我还没有测试过它,看它是否能在子视图上工作。这将显示键盘
InputMethodManager imm = (InputMethodManager)
OrderMainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(txtCustomerId, InputMethodManager.SHOW_IMPLICIT);
如果从侦听器中调用键盘,请输入活动名称,而不是此名称,例如,单击按钮editText.requestFocus();你能在setTextMethod()之后添加这个代码吗?我已经试过了。editText获得了焦点,但直到我触摸屏幕,键盘才出现。可能的答案是:@Shubham!。但这并不是公认的答案。这是第二个答案(即imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);)。如果你写一个答案,我会接受的。看我上面的评论。同样的事情。editText获得了焦点,但键盘上的图标直到我触摸屏幕才出现。问题并不是让它真正聚焦。editText.requestFocus();似乎可以自己完成,但键盘一直隐藏着,直到我实际触摸屏幕为止。android:WindowsOfInputMode=“StateAllwaysVisible”加载项活动声明我以前尝试过,但我无法让键盘始终可见(我不是真的使用xml,因为我的代码相当动态)。我假设我一直在看的代码也可以这样做。给出实际的解决方案。那不是解决方案。。。。。。。第二个答案是它。见我之前的评论。