我们经常使用手机的打电话功能,当我们按键盘的时候,有一个地方显示我们按键的内容,当我们的手点击那个地方的时候,并没有弹出软件盘,所以我们再有数字键盘的时候,要屏蔽系统的软件盘。

我们分析一下,软件盘弹出的条件:

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

最新文章

  1. 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。
  2. java安全沙箱(二)之.class文件检验器
  3. MSBuild Devenv 编译VC 工程
  4. Gitlab的搭建
  5. shiro 标签
  6. 解决vbox下安装centos不能上网问题
  7. PHP 设计模式 笔记与总结(10)数据对象映射模式 2
  8. PHP date函数参数详解
  9. Spark History Server配置使用
  10. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B. Guess the Permutation 水题
  11. PHP常用代码:
  12. ant-学习记录二
  13. 腾讯云安装openvz,高速搭建測试环境
  14. 学好UI你必须要掌握这些技术
  15. 离线缓存 manifest
  16. 关于Android sdkmanager目录结构的总结
  17. angualar2——八大组成
  18. [LeetCode] Most Profit Assigning Work 安排最大利润的工作
  19. gerapy 实现自动化部署
  20. List 集合转换为String

热门文章

  1. Delphi VclSkin使用教程
  2. 修复duilib CEditUI控件和CWebBrowserUI控件中按Tab键无法切换焦点的bug
  3. 【LeetCode】102 - Binary Tree Level Order Traversal
  4. 类似于QQ游戏百万人同时在线的服务器架构实现
  5. [翻译]创建ASP.NET WebApi RESTful 服务(7)
  6. Android实例-实现扫描二维码并生成二维码(XE8+小米5)
  7. WT588D播放合成语音时出现某些语句不能正常播报的情况,经过对比其他语句,看似有点不符合逻辑。
  8. UVA 624 (0 1背包 + 打印路径)
  9. apache配置虚拟主机后,启动速度慢
  10. django admin site配置(二)