2011-04-01 49 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發佈實例。