以编程方式打开软键盘

我有一个没有子窗口小部件的活动和相应的XML文件是,

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="true" > </LinearLayout> 

我想打开软键盘以编程方式开始活动。我现在试过的是,

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } 

给我一些指导。

我已经使用了以下几行来在onclick事件中手动显示软键盘,并且键盘是可见的。

 InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow( linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); 

但是我仍然无法打开这个活动被打开,所以这是任何解决scheme。

在清单文件中,尝试将以下内容添加到活动启动时要显示键盘的<activity>

android:windowSoftInputMode="stateVisible"

这应该会导致键盘在活动启动时变为可见。

有关更多选项,请查看文档 。

请按照下面的代码。 我相信你的问题将得到解决。

 if (imm != null){ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); } 

这是作品

 <activity ... android:windowSoftInputMode="stateVisible" > </activity> 

要么

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

我所需要的只是在一个非常精确的时刻揭开键盘。 这对我有用! 感谢Benites。

  private Handler mHandler= new Handler(); 

而在非常精确的时刻:

  mHandler.post( new Runnable() { public void run() { InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); yourEditText.requestFocus(); } }); 

我已经使用了以下几行来在onclick事件中手动显示软键盘。

 public void showKeyboard(final EmojiconEditText ettext){ ettext.requestFocus(); ettext.postDelayed(new Runnable(){ @Override public void run(){ InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.showSoftInput(ettext,0); } } ,200); } 

把它放在onResume方法中:

 findViewById(R.id.root_view_of_your_activity_layout).post( new Runnable() { public void run() { InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); yourEditText.requestFocus(); } }); 

runnable是需要的,因为当操作系统触发onResume方法时,你不能确定所有的绘制视图,所以从根布局调用的post方法使它等待,直到每个视图准备好。

似乎这是工作

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_patientid); editText = (EditText)findViewById(R.id.selectPatient); //editText.requestFocus(); //works without that } @Override protected void onResume() { findViewById(R.id.selectPatient).postDelayed( new Runnable() { public void run() { editText.requestFocus(); InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT); } },100); super.onResume(); } 

似乎这样做效果更好:在清单中:

 <application> <activity android:name="com.doodkin.myapp.ReportActivity" android:label="@string/title_activity_report" android:screenOrientation="sensor" android:windowSoftInputMode="stateHidden" > // add this or stateVisible </activity> </application> 

似乎在Android 4.2.2清单工作,但不能在Android 4.0.3工作

我用这样的方式来显示软键盘编程,这对我来说是防止在启动键盘时自动调整屏幕大小。

在清单中:

 <activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan"> </activity> 

在XXXActvity:

 EditText et = (EditText))findViewById(R.id.edit_text); Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInputFromWindow(et.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); } }; timer.schedule(task, 200); 

我认为这会节省时间来search这个问题。

onCreate方法的activity或onActivity中创build一个片段

 .... view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { view.removeOnPreDrawListener(this); InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // !Pay attention to return `true` // Chet Haase told to return true; } }); 
 InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 

这是必需的源代码:

 public static void openKeypad(final Context context, final View v) { new Handler().postDelayed(new Runnable() { @Override public void run() { InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); Log.e("openKeypad", "Inside Handler"); } },300);} 

有关详情,请通过此链接。 这帮助了我。 https://github.com/Nikhillosalka/Keyboard/blob/master/README.md

类似于@ShimonDoodkin的回答,这就是我在一个片段中所做的。

https://stackoverflow.com/a/29229865/2413303

  passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen 

ShowKeyboard地方在哪里

 private class ShowKeyboard implements Runnable { @Override public void run() { passwordInput.setFocusableInTouchMode(true); passwordInput.requestFocus(); getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0); } } 

input成功后,我也确定我隐藏了键盘

 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getView().getWindowToken(), 0); 

已经有太多的答案,但除此之外,没有任何工作

 inputMethodManager.showSoftInput(emailET,InputMethodManager.SHOW_FORCED); 

我用showSoftInput使用SHOW_FORCED

而我的活动有

  android:windowSoftInputMode="stateVisible|adjustResize" 

希望这可以帮助别人

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 

在onResume()中使用上面的代码打开软键盘