日期:2014-05-16 浏览次数:20353 次
我们经常使用手机的打电话功能,当我们按键盘的时候,有一个地方显示我们按键的内容,当我们的手点击那个地方的时候,并没有弹出软件盘,所以我们再有数字键盘的时候,要屏蔽系统的软件盘。
我们分析一下,软件盘弹出的条件:
1、焦点,当EditText处于焦点的时候,会自动弹出软件盘,所以我们要重写onFocusChanged函数
2、触摸时间,当你点击EditText的时候,那它就会处于焦点,所以我们要重写onTouchEvent函数
3、当布局改变的时候,EditText也会处于焦点,所以我们也应该重写一下layout函数
package com.jwzhangjie.pjsip.ui.dialpad; import android.content.Context; import android.graphics.Rect; import android.text.InputType; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.accessibility.AccessibilityEvent; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; /** * 数字输入,暂时不支持字母输入所以把软键盘全部屏蔽 * * @author jwzhangjie */ public class DigitsEditText extends EditText { public DigitsEditText(Context context) { super(context); init(); } public DigitsEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DigitsEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //设置一行显示 this.setInputType(InputType.TYPE_NULL); } @Override public boolean onTouchEvent(MotionEvent event) { final boolean ret = super.onTouchEvent(event); // Must be done after super.onTouchEvent() applyKeyboardShowHide(); return ret; } @Override public void sendAccessibilityEventUnchecked(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) { // Since we're replacing the text every time we add or remove a // character, only read the difference. (issue 5337550) final int added = event.getAddedCount(); final int removed = event.getRemovedCount(); final int length = event.getBeforeText().length(); if (added > removed) { event.setRemovedCount(0); event.setAddedCount(1); event.setFromIndex(length); } else if (removed > added) { event.setRemovedCount(1); event.setAddedCount(0); event.setFromIndex(length - 1); } else { return; } } else if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) { // The parent EditText class lets tts read "edit box" when this View // has a focus, which // confuses users on app launch (issue 5275935). return; } super.sendAccessibilityEventUnchecked(event); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); // Here we ensure that we hide the keyboard // Since this will be fired when virtual keyboard this will probably // blink but for now no better way were found to hide keyboard for sure applyKeyboardShowHide(); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused) { applyKeyboardShowHide(); } else { final InputMethodManager imm = ((InputMethodManager) getContext() .getSystemService(Context.INPUT_METHOD_SERVICE)); if (imm != null && imm.isActive(this)) { imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0); } } } private void applyKeyboardShowHide() { final InputMethodManager imm = ((InputMethodManager) getContext() .getSystemService(Context.INPUT_METHOD_SERVICE)); if (imm != null) { if (imm.isActive(this)) { imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0); } } } }