2011-01-13 64 views
18

我讀了幾個其他帖子,並使用input.setInputType(TYPE_NUMBER_FLAG_DECIMAL);打開鍵盤,但它不是數字鍵盤如何顯示數字鍵盤

有沒有這個技巧?

回答

22

android:inputType="number"你的XML文件中

編輯: 「數」 是不行的,改爲 「數量」(小寫字母N)

+0

editview在使用構建器的彈出窗口中,所以沒有xml。我認爲setInputType和在XML中做的一樣 – 2011-01-14 04:31:12

11

一)在XML

android:numeric="decimal" 

b)代碼

EditText editView = new EditText(this); 
editView.setKeyListener(new DigitsKeyListener()); 
5

在你的XML,你必須這樣做:

android:inputType="Number" 

在你的代碼,這樣做:

editText.requestFocus(); 

然後:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

我花了很多天算出這個。我什至嘗試editText.performClick();不工作。

2

彈出的活動開始的數字鍵盤,我用下面的步驟:

創建編輯文本字段中佈局爲:

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