2013-02-01 85 views
0

作为我的应用程序的一部分,我想要在应用程序启动时显示android默认键盘,我从论坛获得了以下代码,但它不工作。如何让键盘在应用程序启动时显示

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); 

请让我知道,如果我做错什么,或是否有任何其他的方式来完成的功能。

在此先感谢

+0

“Activity”中是否有“EditText”? –

+0

@ user9778168解决了吗? –

回答

0

我在做类似的事情。但它需要在布局中使用EditText。

private EditText editText; 

void showKeyboard() { 
    this.editText.requestFocus(); 
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    mgr.showSoftInput(this.editText, InputMethodManager.SHOW_IMPLICIT); 
} 
0

如果您在布局中有一个EditText,使用此:

EditText editText = (EditText) findViewById(R.id.editText); 
editText.requestFocus(); 
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

另外,如果你不在布局的EditText,仍然需要显示的软键盘,使用此:

this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

注:第二个选择,为LayoutParams必要进口是:import android.view.WindowManager.LayoutParams;

0

你只需要在你的manifest.xml文件更改

<activity android:name=".MyActivity" 
android:label="@string/app_name" 
android:windowSoftInputMode="stateAlwaysVisible" /> 

退房this详情。

通过执行此设备的键盘将加载应用程序时打开。

相关问题