Android 如何在应用程序启动时显示键盘
作为我应用程序的一部分,我希望在应用程序启动时显示android默认键盘,我从论坛上获得了以下代码,但它不起作用Android 如何在应用程序启动时显示键盘,android,keypad,Android,Keypad,作为我应用程序的一部分,我希望在应用程序启动时显示android默认键盘,我从论坛上获得了以下代码,但它不起作用 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InputMethodManager imm; imm = = (InputMethodMana
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InputMethodManager imm;
imm = = (InputMethodManager) gettSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, inputMethodManager.HIDE_IMPLICIT_ONLY);
请让我知道,如果我做错了什么,或者是否有任何其他方式来实现功能
提前谢谢我也在做类似的事情。但它需要在布局中使用EditText
private EditText editText;
void showKeyboard() {
this.editText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(this.editText, InputMethodManager.SHOW_IMPLICIT);
}
如果版面中有
EditText
,请使用以下命令:
EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
或者,如果版面中没有EditText
,并且仍然需要显示软键盘,请使用以下方法:
EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
注意:对于第二个备选方案,
LayoutParams
所需的导入是:import android.view.WindowManager.LayoutParams
您只需要在manifest.xml
文件中进行更改
获取详细信息
加载应用程序时,此设备的键盘将打开。在活动中是否有编辑文本
?