带有×的EditText
2015-12-28 12:56
225 查看
代码:
EditTextWithDel.java(直接复制):
package com.sunday.customs; import com.example.customs.R; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.EditText; /** * @author sunday * 2013-12-04 */ public class EditTextWithDel extends EditText { private final static String TAG = "EditTextWithDel"; private Drawable imgInable; private Drawable imgAble; private Context mContext; public EditTextWithDel(Context context) { super(context); mContext = context; init(); } public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } public EditTextWithDel(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(); } private void init() { imgInable = mContext.getResources().getDrawable(R.drawable.delete_gray); imgAble = mContext.getResources().getDrawable(R.drawable.delete); addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) { setDrawable(); } }); setDrawable(); } //设置删除图片 private void setDrawable() { if(length() < 1) setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null); else setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null); } // 处理删除事件 @Override public boolean onTouchEvent(MotionEvent event) { if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) { int eventX = (int) event.getRawX(); int eventY = (int) event.getRawY(); Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY); Rect rect = new Rect(); getGlobalVisibleRect(rect); rect.left = rect.right - 50; if(rect.contains(eventX, eventY)) setText(""); } return super.onTouchEvent(event); } @Override protected void finalize() throws Throwable { super.finalize(); } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.sunday.customs.EditTextWithDel android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="输入" android:padding="7dp" android:singleLine="true" /> <com.sunday.customs.EditTextWithDel android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="输入" android:padding="7dp" android:singleLine="true" /> </LinearLayout>
MainActivity.java:
package com.sunday.customs; import com.example.customs.R; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
相关文章推荐
- Maven以及其Eclipse插件m2eclipse的安装
- 聊聊高并发(三十八)解析java.util.concurrent各个组件(十四) 理解Executor接口的设计
- 正则表达式
- js获取url参数值
- VS2015自定义注释内容
- 为什么说fb_info->node 是设备的次设备号
- 基于Gabor滤波的纹理分割 Texture Segmentation Using Gabor Filters
- (转)onTouchEvent方法的使用
- 大量高清文字版PDF编程书籍下载
- aws EC2 挂载 其它卷(aws EC2 网络配置出错,不能 ssh 连接时,需要启动一个新实例,去挂载 这个出错的卷,来修改卷里的错误配置)
- 排序法(整理)
- Android计时器TimerTask,Timer,Handler
- 深入C#中的事件
- 累加直方图 Cumulative Histogram
- 仿QQ新浪主界面底部导航与中间按钮点击弹出以及返回键点击返回效果
- 日常使用 Git 的 19 个建议
- server2008 r2 系统桌面级使用优化
- 三种方式获得手机屏幕的宽和高
- JMeter(一)在mac下安装Jmeter及问题Unable to access jarfile ./ApacheJMeter.jar 解决
- iOS刷新某个cell时候crash