Android帶清除功能的輸入框控件EditTextWithDel
記錄下一個很實用的小控件EditTextWithDel,就是在Android系統(tǒng)的輸入框右邊加入一個小圖標,點擊小圖標可以清除輸入框里面的內(nèi)容,由于Android原生EditText不具備此功能,所以要想實現(xiàn)這一功能我們需要重寫EditText。
效果圖如下:

主要的思路就是為右邊的圖片設置監(jiān)聽,點擊右邊的圖片清除輸入框的內(nèi)容并隱藏刪除圖標,因為我們不能直接給EditText設置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件,用輸入框的的onTouchEvent()方法來模擬.
package com.xiaolijuan.edittextwithdeldome;
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: adan
* @description: 自定義帶有刪除功能的EditText
* @projectName: EditTextWithDelDome
* @date: 2016-02-28
* @time: 23:34
*/
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() {
imgAble = mContext.getResources().getDrawable(
R.mipmap.icon_delete_gray);
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, null, 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();
}
}
setDrawable()方法,setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)來在上、下、左、右設置圖標,如果不想在某個地方顯示,則設置為null。
接下來我們來使用它設置Activity的布局,一個我們自定義的輸入框,一個按鈕
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="25dp" android:background="#ffffff"> <ImageView android:id="@+id/img" android:layout_width="25dp" android:layout_height="30dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_margin="5dp" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="#56AB55" /> <com.xiaolijuan.edittextwithdeldome.EditTextWithDel android:id="@+id/et_phoneNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_margin="2dp" android:layout_toRightOf="@+id/img" android:background="#ffffff" android:hint="請輸入手機號碼" android:maxLength="11" android:phoneNumber="true" android:singleLine="true" /> </RelativeLayout> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="25dp" android:background="#56AB55" android:text="確定" /> </LinearLayout>
然后就是界面代碼的編寫,主要測試下輸入框
package com.xiaolijuan.edittextwithdeldome;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.et_phoneNumber);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(userName.getText().toString())){
Toast.makeText(getApplicationContext(),"手機號碼為空",Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(getApplicationContext(),userName.getText().toString(),Toast.LENGTH_LONG).show();
}
});
}
}
源碼下載:http://xiazai.jb51.net/201609/yuanma/EditTextWithDel(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 自定義彈性ListView控件實例代碼(三種方法)
關于在Android中實現(xiàn)ListView的彈性效果,有很多不同的方法,網(wǎng)上一搜,也有很多,下面貼出在項目中經(jīng)常用到的兩種實現(xiàn)ListView彈性效果的方法(基本上拿來就可以用),需要的朋友參考下本段代碼2016-01-01
AndroidStudio 使用過程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說明,如何處理出現(xiàn)這種問題,有需要的小伙伴可以參考下2016-09-09
Android開發(fā)之DatePicker和TimePicker實現(xiàn)選擇日期時間功能示例
這篇文章主要介紹了Android開發(fā)之DatePicker和TimePicker實現(xiàn)選擇日期時間功能,結合實例形式分析了Android DatePicker和TimePicker組件的功能、常用函數(shù)、布局及日期時間選擇相關操作技巧,需要的朋友可以參考下2019-03-03
ViewModel中StateFlow和SharedFlow單元測試使用詳解
這篇文章主要為大家介紹了ViewModel中StateFlow和SharedFlow單元測試使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟
這篇文章主要介紹了Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

