Android中EditText點擊獲得焦點後無法顯示輸入法鍵盤
【背景】
android中EditText的Enable已經設置為True了,表示可以被編輯,
但是點擊輸入框,獲得焦點後,無法顯示輸入法,導致無法輸入內容。
比如:
Descriptor的值是EditText
之前已經設置為可編輯了:
1
2
|
EditText
variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
variableValueView.setEnabled( true );
|
當前值是DESCRIPT,點擊後但是不顯示輸入法,所以沒法修改想要的值
【折騰過程】
1.搜:
android edittext cannot input
找了些:
cannot input text into EditText widgets inside ListView – Google Groups
但是都沒用
2.注意到,之前加了listener:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
OnFocusChangeListener
mFocusChangedListener;
variableValueView.setOnFocusChangeListener(mFocusChangedListener);
mFocusChangedListener
= new OnFocusChangeListener()
{
@Override
public void onFocusChange(View
v, boolean hasFocus)
{
//
RelativeLayout parentView = (RelativeLayout)v.getParent();
//
TextView labelView = (TextView) parentView.findViewById(R.id.variableLabel);
//
String labelStr = (String) labelView.getText();
if (hasFocus){
//enter
into
//Toast.makeText(getApplicationContext(),
"got focus: " + v.toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(),
"got focus: " + labelStr, Toast.LENGTH_LONG).show();
} else {
//left
//Toast.makeText(getApplicationContext(),
"lost focus: " + v.toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(),
"lost focus: " + labelStr, Toast.LENGTH_LONG).show();
//
EditText valueView = (EditText)v;
//
if(valueView.isEnabled()){
//
//only validate new value for editable value
//
if(mVarValueViewVarNameMap.containsKey(valueView)){
//
String varName = mVarValueViewVarNameMap.get(valueView);
//
String varValue = (String)valueView.getText().toString();
//
DeviceModelManager.getInstance().getCurrentDeviceModel().postvalidateVariablesvalue(varName, varValue);
//
}
//
}
}
}
};
|
現在試試,去掉Listener:
1
|
//variableValueView.setOnFocusChangeListener(mFocusChangedListener);
|
看看效果:結果還是不行,點擊到可以編輯的EditText中後,還是不能顯示出輸入法。
3.再去搜:
android edittext not show keyboard
有空再去試試:
android – Custom EditText is not showing keyboard on focus – Stack Overflow
的:
1
|
android:focusable="true"
|
4.另外參考:
java – Programatically Hide/Show Android Soft Keyboard – Stack Overflow
去試試,給我此處的RelativeLayout
加上:
1
|
android:focusableInTouchMode="true"
|
PS:忘了說了,之前是可以正常顯示出輸入法的。。。不知道為何現在不能顯示,也搞不清有哪些改動可能導致此問題的。
結果還是不行。
5.突然想到,難道是當前正在測試的三星的PAD(GT-P5210)有問題?所以去重啟PAD試試,結果問題依舊。
6.去掉上麵的
android:focusableInTouchMode
試試,結果問題依舊。
7.現在把之前的RelativeLayout中所有的EditText的Enable都改為True看看效果。
記得發現詭異的問題:
在有多個的EditText的情況下,前麵幾個的InputType都是
TYPE_TEXT_VARIATION_NORMAL
然後切換到
TYPE_CLASS_NUMBER
結果輸入法就顯示出來了,此時隻允許輸入數字
然後再切換到別的
TYPE_TEXT_VARIATION_NORMAL
的,輸入法就可以正常出現,且允許輸入各種字符了。
即:
第一次,焦點點擊到String部分的EditText,結果都不能出現輸入法
隻有先去點擊別的Number的,再切換回String的,輸入法才正常,才可以輸入。。。
所以再去把:
1
|
int inputType
= InputType.TYPE_TEXT_VARIATION_NORMAL;
|
換為別的值,但是想要在換之前,再去確認各種可能的類型。
然後搜:
android TYPE_TEXT_VARIATION_NORMAL not show
參考:
android – Show the password with EditText – Stack Overflow
果然和我想的一樣,感覺應該用bit or的:
1
|
inputType
= InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
|
看看效果,結果真的就可以了:
當EditText獲得焦點後,就自動顯示出輸入法,可以輸入內容了。而且光標也能顯示出來了(之前連光標也沒有的)
如圖:
【總結】
對於EditText的話,其InputType屬性
如果設置為
1
|
InputType.TYPE_TEXT_VARIATION_NORMAL
|
(對應的xml定義中是)
則會導致
可以點擊對應的EditText,獲得焦點
但是無法顯示輸入法,無法顯示光標
改為:
1
|
InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_NORMAL;
|
就可以:
在獲得焦點後,顯示對應的輸入法和光標了。
【引申】
1.參考了官網的解釋:
InputType | Android Developers
“
A time field:
inputType = TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_TIME”
再去把之前錯寫為:
1
|
inputType
= InputType.TYPE_DATETIME_VARIATION_TIME;
|
改為:
1
|
inputType
= InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME;
|
2.又從:
Android programmatically disable autocomplete/autosuggest for EditText in emulator – Stack Overflow
的
textVisiblePassword
找到:
可知:
前麵所說的
1
|
InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_NORMAL;
|
對應的xml中的定義:
EditText中的屬性
1
|
android:inputType="text"
|
而其他不同類型,可以查看官網即可得到:
【後記】
關於EditText的InputType的更詳細的解釋,可參考後來的總結:
【整理】Android中EditText(或TextView)中的InputType類型含義與如何定義
現在整理如下:
EditText的InputType屬性,可以在代碼中設置,也可以預先在xml中定義
設置EditText的InputType屬性,最簡單省事的辦法就是在定義EditText的xml中直接設置。
比如:
想要設置一個可編輯的文本框的輸入內容為隻能輸入數字,則就可以:
(1)xml中定義InputType為number
1
2
3
4
|
< EditText android:id = "@+id/variableValue"
......
android:inputType = "number" />
|
(2)代碼中設置InputType為TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL
1
2
3
|
EditText
variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
int inputType
= InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
variableValueView.setInputType(inputType);
|
這樣的話,之後界麵中生成的EditText,當點擊後要輸入內容的時候,彈出的輸入法,自動變成那種隻能輸入數字的小鍵盤類型的了:
另外,附上,正常的普通字符串,即:
xml中:
1
|
android:inputType="text"
|
或代碼中:
1
|
someEditText.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_NORMAL);
|
時,顯示出來的輸入法鍵盤的效果:
EditText的InputType屬性對應的xml定義有哪些,以及代碼中設置的InputType類型有哪些
知道了設置EditText的InputType屬性值,既可以通過xml中定義,也可以在代碼中設置為InputType的某種值,但是到底這些值有哪些,以及分別對應的含義是啥,則可以參考官網:
TextView | Android Developers – android:inputType
中的完整的列表:
Constant |
Value |
Description |
none |
0×00000000 |
There is no content type. The text is not editable. |
text |
0×00000001 |
Just plain old text. Corresponds toTYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_NORMAL. |
textCapCharacters |
0×00001001 |
Can be combined with text and its variations to request capitalization of all characters. Corresponds toTYPE_TEXT_FLAG_CAP_CHARACTERS. |
textCapWords |
0×00002001 |
Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds toTYPE_TEXT_FLAG_CAP_WORDS. |
textCapSentences |
0×00004001 |
Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds toTYPE_TEXT_FLAG_CAP_SENTENCES. |
textAutoCorrect |
0×00008001 |
Can be combined with text and its variations to request auto-correction of text being input. Corresponds toTYPE_TEXT_FLAG_AUTO_CORRECT. |
textAutoComplete |
0×00010001 |
Can be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds toTYPE_TEXT_FLAG_AUTO_COMPLETE. |
textMultiLine |
0×00020001 |
Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds toTYPE_TEXT_FLAG_MULTI_LINE. |
textImeMultiLine |
0×00040001 |
Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds toTYPE_TEXT_FLAG_IME_MULTI_LINE. |
textNoSuggestions |
0×00080001 |
Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to TYPE_TEXT_FLAG_NO_SUGGESTIONS. |
textUri |
0×00000011 |
Text that will be used as a URI. Corresponds toTYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_URI. |
textEmailAddress |
0×00000021 |
Text that will be used as an e-mail address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_ADDRESS. |
textEmailSubject |
0×00000031 |
Text that is being supplied as the subject of an e-mail. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_SUBJECT. |
textShortMessage |
0×00000041 |
Text that is the content of a short message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_SHORT_MESSAGE. |
textLongMessage |
0×00000051 |
Text that is the content of a long message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_LONG_MESSAGE. |
textPersonName |
0×00000061 |
Text that is the name of a person. Corresponds toTYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_PERSON_NAME. |
textPostalAddress |
0×00000071 |
Text that is being supplied as a postal mailing address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_POSTAL_ADDRESS. |
textPassword |
0×00000081 |
Text that is a password. Corresponds toTYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_PASSWORD. |
textVisiblePassword |
0×00000091 |
Text that is a password that should be visible. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_VISIBLE_PASSWORD. |
textWebEditText |
0x000000a1 |
Text that is being supplied as text in a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EDIT_TEXT. |
textFilter |
0x000000b1 |
Text that is filtering some other data. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_FILTER. |
textPhonetic |
0x000000c1 |
Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_PHONETIC. |
textWebEmailAddress |
0x000000d1 |
Text that will be used as an e-mail address on a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS. |
textWebPassword |
0x000000e1 |
Text that will be used as a password on a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_PASSWORD. |
number |
0×00000002 |
A numeric only field. Corresponds toTYPE_CLASS_NUMBER |TYPE_NUMBER_VARIATION_NORMAL. |
numberSigned |
0×00001002 |
Can be combined with number and its other options to allow a signed number. Corresponds toTYPE_CLASS_NUMBER |TYPE_NUMBER_FLAG_SIGNED. |
numberDecimal |
0×00002002 |
Can be combined with number and its other options to allow a decimal (fractional) number. Corresponds to TYPE_CLASS_NUMBER |TYPE_NUMBER_FLAG_DECIMAL. |
numberPassword |
0×00000012 |
A numeric password field. Corresponds toTYPE_CLASS_NUMBER |TYPE_NUMBER_VARIATION_PASSWORD. |
phone |
0×00000003 |
For entering a phone number. Corresponds toTYPE_CLASS_PHONE. |
datetime |
0×00000004 |
For entering a date and time. Corresponds toTYPE_CLASS_DATETIME |TYPE_DATETIME_VARIATION_NORMAL. |
date |
0×00000014 |
For entering a date. Corresponds toTYPE_CLASS_DATETIME |TYPE_DATETIME_VARIATION_DATE. |
time |
0×00000024 |
For entering a time. Corresponds toTYPE_CLASS_DATETIME |TYPE_DATETIME_VARIATION_TIME. |
如此,就可以自己去在xml或代碼中,分別試試,每種不同的InputType對應的都是什麼效果了。
注意:通過代碼給InputType賦值時,不是設置TYPE_XXX_VARIATION_YYY,而是要設置TYPE_CLASS_XXX | TYPE_XXXX_VARAITION_YYY
之前在代碼中給InputType設置值,錯寫成:
1
|
inputType
= InputType.TYPE_DATETIME_VARIATION_TIME;
|
導致,EditText點擊後,不顯示輸入法鍵盤,改為正確的:
1
|
inputType
= InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME;
|
就可以正常的顯示鍵盤了。
而後,也注意到官網
InputType | Android Developers
的解釋中的示例:
|
以及
TextView | Android Developers – android:inputType
中提示的:
“Must be one or more (separated by ‘|’) of the following constant values.”
即:
需要一個或多個值,中間通過豎杠"|"去抑或(按位或)的。
最後更新:2017-04-03 07:57:10