2011-04-01 127 views
15

我创建了一个简单的应用程序来测试以下功能。当我的活动启动时,需要在软键盘打开的情况下启动。Android showSoftInput软键盘无法使用?

我的代码不起作用?!

我已经尝试清单中的各种“状态”设置和InputMethodManager(imm)代码中的不同标志。

我已将该设置包含在AndroidManifest.xml中,并在唯一活动的onCreate中显式调用。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.mycompany.android.studyIme" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="7" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".StudyImeActivity" 
        android:label="@string/app_name" 
        android:windowSoftInputMode="stateAlwaysVisible"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

...主要布局(main.xml中)...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     /> 
    <EditText 
     android:id="@+id/edit_sample_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hello" 
     android:inputType="textShortMessage" 
    /> 
</LinearLayout> 

...和...代码

public class StudyImeActivity extends Activity { 
    private EditText mEditTextStudy; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mEditTextStudy = (EditText) findViewById(R.id.edit_study); 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.showSoftInput(mEditTextStudy, InputMethodManager.SHOW_FORCED); 
    } 
} 
+0

嗯...我刚刚在我的Sprint LG Optimus手机上试过这个,只有默认的“HelloWorld”活动(即不包括SHOW_FORCED代码),并且按预期工作。该功能是否可能与设备(操作系统安装)相关?当我回家时,我会再次在我的其他设备(HTC,G2和MyTouch)上进行测试。 – mobibob 2011-04-02 21:10:30

+0

请在这里看到我的回复,我已经尝试了所有下面提到的技术,但这工作:http://stackoverflow.com/a/37529370/3900270 – strangetimes 2016-05-30 15:39:38

回答

15

这是如此微妙,那这是犯罪。这适用于而不是有一个硬滑出式键盘的手机。通过此呼叫,带有硬键盘的手机不会自动打开。我的LG和老的Nexus One没有键盘 - 因此,当活动启动时(这是我想要的),软键盘会打开,但带滑出式键盘的MyTouch和HTC G2手机无法打开软键键盘,直到我在硬键盘关闭的情况下触摸编辑区域。

+1

注意:我已经做了很多试验EditText和InputMethodManager试图强制软键盘打开时,当设备有硬键盘没有成功。 – mobibob 2011-04-03 00:12:51

+0

对于任何人在Visual Studio中进行Xamarin开发,在AVD管理器中,您可以编辑您的AVD,并且有一个标记为“硬件键盘存在”的设置。取消选中将允许显示软输入。 – 2016-11-07 18:46:27

18

嘿,我希望你仍然在寻找答案,因为我在测试我的代码时发现它。这里是代码:

InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE); 

imm.toggleSoftInput(0, 0); 

这里是我的疑问,得到的回答是: android - show soft keyboard on demand

15

这个工作跟我硬键盘在手机上:

editText1.requestFocus(); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
+1

伟大的我失踪了requestFocus()这是停止键盘显示第一次。 – 2015-09-10 06:01:44

20

当活动启动似乎键盘最初显示但被其他东西隐藏,因为以下工作(但实际上是一个肮脏的变通):

第一种方法

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
editText.postDelayed(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     editText.requestFocus(); 
     imm.showSoftInput(editText, 0); 
    } 
}, 100); 

方法二

中的onCreate推出它的活动创造

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() 
    { 
    // InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    // inputMethodManager.toggleSoftInputFromWindow(EnterYourViewHere.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); 

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

第三种方法 ADD定的代码在清单活动代码。它会在启动时显示键盘,并将第一个焦点设置为您的愿望视图。

android:windowSoftInputMode="stateVisible" 
+1

我有同样的问题。调用'showSoftInput()'立即没有可见的结果,但在延迟上发布会导致键盘正确显示。我最初认为,就像你一样,它被显示出来,然后被其他东西迅速隐藏起来。经过一点点挖掘,我发现我可以传入一个'ResultReceiver'并记录结果。当我在延迟上发布'showSoftInput()'时,返回给接收者的结果代码是'RESULT_SHOWN'。当我不使用延迟时,我的接收器根本不会被调用。现在我怀疑它不是被隐藏起来的,而是因为某种原因没有显示它。 – 2015-01-09 17:37:35

+1

谢谢你。在解决键盘问题时使用第一种方法,而不是在第一次启动对话框片段(使用EditText)时显示。 – Ugo 2015-12-16 15:38:31

+3

调用'postDelayed'的另一个投票与vs.'后' - 有可能是其他一些默认的隐藏功能,导致键盘首先被隐藏。显示和隐藏键盘是现存最严重的API。 – rmirabelle 2016-06-02 15:08:28

1

我的代码有切换,但不是postDelayed。我曾尝试postDelayed为showSoftInput没有成功,我从那以后尝试过你的建议解决方案。我正要放弃它作为另一个失败的潜在解决方案,直到我决定增加延迟时间。它一直工作到200毫秒,至少在物理电话上不起作用。所以,在你可怜的android开发人员抛弃这个答案之前,请尝试加快解决方案的延迟。它可能会支付添加一些较老的手机。感谢堆,正在为此工作数小时。

4

这个答案可能晚了,但它对我来说完全适用。也许这可以帮助别人:)

public void showSoftKeyboard(View view) { 
    if (view.requestFocus()) { 
     InputMethodManager imm = (InputMethodManager) 
       getSystemService(Context.INPUT_METHOD_SERVICE); 
     boolean isShowing = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); 
     if (!isShowing) 
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
    } 
} 

取决于你的需要,你可以使用其他标志

InputMethodManager.SHOW_FORCED 
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
1

显示软键盘是个大问题。我搜查了很多,以得出最终结论。由于这个答案,给了一些蛛丝马迹:https://stackoverflow.com/a/16882749/5903344

问题:

通常我们只要我们初始化意见称showSoftInput。在活动中,这主要在onCreate中,在片段onCreateView中。为了显示键盘,IMM需要使focsedView处于活动状态。这可以使用IMM的isActive(查看)方法进行检查。如果我们在创建视图时调用showSoftInput,则IMM很可能不会激活该视图。这就是为什么有时延迟50-100毫秒的showSoftInput有用的原因。但是,这仍然不能保证在100 ms后视图将变为活动状态。所以在我的理解中,这又是一个黑客。

解决方案:

我用下面的类。这将保持每100毫秒运行一次,直到成功显示键盘。它在每次迭代中执行各种检查。一些检查可以停止可运行,一些检查会在100 ms后发布。

public class KeyboardRunnable extends Runnable 
{ 
    // ----------------------- Constants ----------------------- // 
    private static final String TAG = "KEYBOARD_RUNNABLE"; 

    // Runnable Interval 
    private static final int INTERVAL_MS = 100; 

    // ----------------------- Classes ---------------------------// 
    // ----------------------- Interfaces ----------------------- // 
    // ----------------------- Globals ----------------------- // 
    private Activity parentActivity = null; 
    private View targetView = null; 

    // ----------------------- Constructor ----------------------- // 
    public KeyboardRunnable(Activity parentActivity, View targetView) 
    { 
     this.parentActivity = parentActivity; 
     this.targetView = targetView; 
    } 

    // ----------------------- Overrides ----------------------- // 
    @Override 
    public void run() 
    { 
     // Validate Params 
     if ((parentActivity == null) || (targetView == null)) 
     { 
      Dbg.error(TAG, "Invalid Params"); 
      return; 
     } 

     // Get Input Method Manager 
     InputMethodManager imm = (InputMethodManager) parentActivity.getSystemService(Context.INPUT_METHOD_SERVICE); 

     // Check view is focusable 
     if (!(targetView.isFocusable() && targetView.isFocusableInTouchMode())) 
     { 
      Dbg.error(TAG, "Non focusable view"); 
      return; 
     } 
     // Try focusing 
     else if (!targetView.requestFocus()) 
     { 
      Dbg.error(TAG, "Cannot focus on view"); 
      Post(); 
     } 
     // Check if Imm is active with this view 
     else if (!imm.isActive(targetView)) 
     { 
      Dbg.error(TAG, "IMM is not active"); 
      Post(); 
     } 
     // Show Keyboard 
     else if (!imm.showSoftInput(targetView, InputMethodManager.SHOW_IMPLICIT)) 
     { 
      Dbg.error(TAG, "Unable to show keyboard"); 
      Post(); 
     } 
    } 

    // ----------------------- Public APIs ----------------------- // 
    public static void Hide(Activity parentActivity) 
    { 
     if (parentActivity != null) 
     { 
      InputMethodManager imm = (InputMethodManager) parentActivity.getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(parentActivity.findViewById(android.R.id.content).getWindowToken(), 0); 
     } 
     else 
     { 
      Dbg.error(TAG, "Invalid params to hide keyboard"); 
     } 
    } 

    // ----------------------- Private APIs ----------------------- // 
    protected void Post() 
    { 
     // Post this aftr 100 ms 
     handler.postDelayed(this, INTERVAL_MS); 
    } 
} 

要使用这个,只需创建这个类的一个实例。将父活动和targetView传递给键盘输入和焦点。然后使用Handler发布实例。