![](/img/trans.png)
[英]Is there a way to show the numbers first on the soft keyboard for Android?
[英]Android Show Soft Keyboard When First Activity Starts?
我需要在应用程序启动时显示虚拟键盘,但到目前为止我都失败了。
我在方法“OnCreate”中使用此代码来显示虚拟键盘
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);
此代码可以随时在任何屏幕上正常工作,但在“第一个”活动开始时不起作用。 为什么?
当我开始另一个活动时它尝试了它并且它可以工作,但是当我开始“第一个”活动时它不起作用。
我试图把这个代码放在事件“OnCreate”等等......但它似乎不起作用。
无论如何,当我启动应用程序时,“强制”显示键盘?
提前致谢。
我找到了解决方案:
txtPassword.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtPassword, 0);
}
},200);
谢谢 !!!
如果活动首先来自后台,则不会调用onCreate。 您是否尝试过将该代码放入onResume?
只有在活动首次启动或活动被终止且用户再次导航到活动时才会调用onCreate。 因此,如果活动仍处于活动状态但在后台,则不会调用onCreate。
另一方面,每次活动从背景到达前景(在屏幕上可见)时,都会调用onResume。
如果您对http://developer.android.com/reference/android/app/Activity.html感兴趣,可以在此链接到活动生命周期。
希望能帮助到你。
我面对同样的问题,下面这个方法帮助了我
public static void showKeyboard(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.