EditText自定义监听方法实现onFocusChanged的监听
来源:互联网 发布:jdk 1.7 linux 64 编辑:程序博客网 时间:2024/05/14 06:06
<div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><span style="font-weight: bold;"><span style="line-height: 15.4px;"> 有一定开发经验的程序员知道,EditText控件位于一个Activity的布局最上方时,当<span style="line-height: 15.4px;"><span style="line-height: 15.4px;">EditText实现onFocusChanged的监听方法时,</span></span></span></span></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><span style="font-weight: bold;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;">程序运行的时候,输入光标会自动定位到EditText控件中,这时会触发<span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;">onFocusChanged监听,执行里面的方法。这会导致一些麻烦</span></span></span></span></span></span></span></span></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><strong>(例如空指针)。针对这种情况该怎样解决呢?下面的一个例子利用继承和接口回调实现了onFocusChanged监听方法。</strong></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><strong> 具体步骤如下:</strong></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><strong> 一、写一个MyEditText类继承EditText,定义一个内部类接口<span style="line-height: 15.4px;">OnImputCompleteListener,该接口具有<code class="java keyword" style="list-style: none; font-size: 1em !important; line-height: 1.1em !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; padding: 0px !important; border-radius: 0px !important; margin: 0px !important; border: 0px !important; outline: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background: none !important;">void</code><span style="color: rgb(51, 51, 51); line-height: 15.4px; white-space: pre;"> </span><code class="java plain" style="list-style: none; font-size: 1em !important; line-height: 1.1em !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; padding: 0px !important; border-radius: 0px !important; margin: 0px !important; border: 0px !important; outline: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background: none !important;">onImputComplete()</code></span></strong></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><strong>方法.</strong></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="color: rgb(0, 102, 153);"><span style="white-space: nowrap;"><strong> 二 、在MyEditText中添加<code class="java keyword" style="list-style: none; font-size: 1em !important; line-height: 1.1em !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; padding: 0px !important; border-radius: 0px !important; margin: 0px !important; border: 0px !important; outline: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background: none !important;">private</code><span style="color: rgb(51, 51, 51); line-height: 15.4px; white-space: pre;"> </span><code class="java plain" style="list-style: none; font-size: 1em !important; line-height: 1.1em !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; padding: 0px !important; border-radius: 0px !important; margin: 0px !important; border: 0px !important; outline: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background: none !important;">OnImputCompleteListener mOnImputCompleteListener属性;</code></strong></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="white-space: nowrap; color: rgb(0, 102, 153); line-height: 15.4px;">添加<span style="line-height: 15.4px;">setOnImputCompleteListener<span style="line-height: 15.4px;">(OnImputCompleteListener </span><span style="line-height: 15.4px;">onImputCompleteListener</span><span style="line-height: 15.4px;">)</span>方法,</span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="white-space: nowrap; color: rgb(0, 102, 153); line-height: 15.4px;"><span style="line-height: 15.4px;">使MyEditText具有<span style="line-height: 15.4px;"><span style="line-height: 15.4px;">OnImputCompleteListener类的的属性,并且能够初始化。</span></span></span></span></div><div class="line number1 index0 alt2" style="list-style: none; line-height: 15.4px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; box-sizing: content-box !important; margin: 0px !important; padding: 0px 1em !important; border: 0px !important; outline: 0px !important; border-radius: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><span style="white-space: nowrap; color: rgb(0, 102, 153); line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;"> 三、在重写的</span></span></span><span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;">onFocusChanged中实现<span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><span style="line-height: 15.4px;">OnImputCompleteListener接口的<span style="line-height: 15.4px;"><span style="line-height: 15.4px;"><code class="java plain" style="list-style: none; font-size: 1em !important; line-height: 1.1em !important; box-sizing: content-box !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; padding: 0px !important; border-radius: 0px !important; margin: 0px !important; border: 0px !important; outline: 0px !important; bottom: auto !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; background: none !important;">onImputComplete()方法。具体代码如下:</code></span></span></span></span></span></span></span></span></span></span></div>
public class MyEditText extends EditText{ private OnImputCompleteListener mOnImputCompleteListener; public MyEditText(Context context) { super(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @SuppressLint("NewApi") public MyEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (!focused) { mOnImputCompleteListener.onImputComplete(); } } public void setOnImputCompleteListener(OnImputCompleteListener onImputCompleteListener) { this.mOnImputCompleteListener = onImputCompleteListener; } } interface OnImputCompleteListener { void onImputComplete();}
0 0
- EditText自定义监听方法实现onFocusChanged的监听
- Android实现EditText输入监听的方法
- EditText自定义监听的案例
- EditText内容的动态监听方法
- 使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听
- EditText的监听事件
- 监听EditText的变化
- 监听EditText的变化
- 监听EditText的输入
- 监听EditText的变化
- 监听EditText的变化
- 监听EditText的变化
- EditText的监听
- 监听EditText的变化
- EditText监听
- EditText监听
- 监听EditText
- 监听EditText的变化--TextWatcher
- ubuntu安装配置FTP
- 51nod 1050 循环数组最大子段和
- jQuery的each、siblings的基本使用和jQ常见错误
- VC++编译说明
- [转]swift 计算年龄,No "|" candidates produce the expected contextual result
- EditText自定义监听方法实现onFocusChanged的监听
- Android字体大小多屏幕适配
- Activity透明设置,且可以实现无标题,亲测可用
- 第26节--层次聚类(上)
- SpringMVC+MyBatis 返回时间格式转换的解决方案
- 51nod 1051 最大子矩阵和
- Java 如何有效地避免OOM:善于利用软引用和弱引用
- ---Filter ( 三 ) web.xml中dispatcher节点的作用
- NOIP2012提高组复赛题4题