2012-03-19 60 views
2

有沒有辦法以編程方式告訴機器人何時由一個EditText獲得的焦點打開鍵盤?怎麼能我的Android應用程序顯示鍵盤

也就是有沒有辦法來告訴它打開數字鍵盤?

感謝 維克多

+0

的可能重複[打開軟鍵盤編程(http://stackoverflow.com/questions/5593053/open-soft-keyboard-programmatically) – Vincent 2014-04-30 10:18:16

回答

4

顯示鍵盤:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
inputMethodManager.showSoftInput(viewToEdit, 0); 

隱藏鍵盤:

if (getCurrentFocus() != null) { 
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0); 
} 
+0

這工作一般,但我沒有提到我的EditText上是一個對話框,並儘快我彈出對話框,鍵盤隱藏。如果我先調出對話框,鍵盤不顯示 - 有什麼想法? – 2012-03-21 02:55:33

5

,使其數值,使用該

text.setInputType(InputType.TYPE_CLASS_NUMBER); 

而且據我所知,在需要時自動鍵盤就會彈出

8

彈出一個數字鍵盤上的活動的開始,你可以按照下列步驟操作:

創建編輯文本字段中佈局爲:(不如果你想有一個QWERTY鍵盤

<EditText 
     ... 
     android:inputType="number"  
     ... /> 

在功能所需的onCreate()顯示軟鍵盤

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 

最重要的是要聚焦編輯的onResume法文本。

@Override 
    public void onResume() { 
     super.onResume(); 
     editText.setFocusableInTouchMode(true); 
     editText.requestFocus(); 
    } 
+0

我試過這段代碼。活動負載時,我可以看到重點的EditText,但我看不到數字鍵盤,直到我點擊的EditText。我錯過了什麼。 – j10 2017-05-28 05:46:05

+0

的的EditText將採取焦點,但你必須迫使鍵盤上來。你可以在onCreate()中寫入該部分。 – 2017-05-28 08:56:21

相關問題