這幾天需要弄一個(gè)短信驗(yàn)證碼輸入框,輸入框是一個(gè)繼承自EditText的ClearableEditText,輸入時(shí)候用的鍵盤是自定義的一個(gè)鍵盤,也就是說點(diǎn)擊EditText的時(shí)候不能彈出系統(tǒng)鍵盤.
如果按正常邏輯 我們會(huì)在代碼中直接設(shè)置EditText的InputType為InputType.TYPE_NULL 即
mSmsCodeInputEditText.setInputType(InputType.TYPE_NULL);
這時(shí)候我們發(fā)現(xiàn)EditText上那個(gè)一閃一閃的光標(biāo)沒了.這樣的顯示效果不夠友好.
解決方法:
在xml布局文件中
android:inputType="none"
代碼中設(shè)置:
mSmsCodeInputEditText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = mSmsCodeInputEditText.getInputType();
mSmsCodeInputEditText.setInputType(InputType.TYPE_NULL);
mSmsCodeInputEditText.onTouchEvent(event);
mSmsCodeInputEditText.setInputType(inType);
CharSequence text = mSmsCodeInputEditText.getText();
if (text instanceof Spannable) {
Spannable spanText = (Spannable) text;
Selection.setSelection(spanText, text.length());
}
return true;
}
});
即可完美解決問題.在點(diǎn)擊的時(shí)候執(zhí)行onTouchEvent之前將其InputType設(shè)置位null這時(shí)候鍵盤不會(huì)彈出來了,執(zhí)行完onTouchEvent(彈鍵盤的操作就是在onTouchEvent)中,我們可以查看TextView的源碼
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
if (mEditor != null) mEditor.onTouchEvent(event);
final boolean superResult = super.onTouchEvent(event);
/*
* Don't handle the release after a long press, because it will
* move the selection away from whatever the menu action was
* trying to affect.
*/
if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
mEditor.mDiscardNextActionUp = false;
return superResult;
}
final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) &&
(mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();
if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
&& mText instanceof Spannable && mLayout != null) {
boolean handled = false;
if (mMovement != null) {
handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
}
final boolean textIsSelectable = isTextSelectable();
if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
// The LinkMovementMethod which should handle taps on links has not been installed
// on non editable text that support text selection.
// We reproduce its behavior here to open links for these.
ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
getSelectionEnd(), ClickableSpan.class);
if (links.length > 0) {
links[0].onClick(this);
handled = true;
}
}
if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
// Show the IME, except when selecting in read-only text.
final InputMethodManager imm = InputMethodManager.peekInstance();
viewClicked(imm);
if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {
handled |= imm != null && imm.showSoftInput(this, 0);
}
// The above condition ensures that the mEditor is not null
mEditor.onTouchUpEvent(event);
handled = true;
}
if (handled) {
return true;
}
}
return superResult;
}
看上面倒數(shù)第二個(gè)if,彈鍵盤的操作就在這個(gè)if判斷語句里 imm.showSoftInput(this, 0);
再看isTextEditable()的實(shí)現(xiàn):
boolean isTextEditable() {
return mText instanceof Editable && onCheckIsTextEditor() && isEnabled();
}
@Override
public boolean onCheckIsTextEditor() {
return mEditor != null && mEditor.mInputType != EditorInfo.TYPE_NULL;
}
可以看到如果mInputType是TYPE_NULL(0x00000000),條件不成立,鍵盤不彈起來.
當(dāng)執(zhí)行完onTouchEvent之后再將EditText的InputType設(shè)置為原先的類型.
注意: 在xml布局文件中設(shè)置EditText的inputType為none(android:inputType="none"),它的實(shí)際值和TYPE_NULL是不一樣的. 所以不要將在xml文件中設(shè)置android:inputType="none" 的效果等同于在代碼中設(shè)置text.setInputType(InputType.TYPE_NULL); 這兩個(gè)設(shè)置完之后EditText的getInputType得到的值是不一樣的.
android:inputType="none" getInputType的值對應(yīng) 0x00020001
text.setInputType(InputType.TYPE_NULL); 對應(yīng)0x00000000