2012-03-19 61 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

相关问题