Android Studio EditText點擊圖標(biāo)清除文本內(nèi)容的實例解析
這篇文章是繼自定義EditText樣式之后的功能強化,對于實際應(yīng)用項目有很大的參考意見,感興趣的朋友可以移步上一篇,”Android Studion自定義EditText樣式”.具體清除EditText文本內(nèi)容功能代碼如下:
package com.liheng; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.EditText; import myself.myapplication.R; /** * 第一步: * 創(chuàng)建一個類,并繼承EditText這個類,實現(xiàn)父類的三個構(gòu)造函數(shù) * 我們只用實現(xiàn)帶一個參數(shù),兩個參數(shù),三個參數(shù)的構(gòu)造函數(shù),帶四個參數(shù)的暫且不管 */ public class MyEditText extends EditText { /** * 第二步: * 聲明3個變量: 兩個圖片對象(變量的值是通過實例化對象得到的, * 在JAVA世界里,除了基本數(shù)據(jù)類型和靜態(tài)成員不是對象外, * 其他一切都是對象.類也是一個對象,類是Class類的對象,圖片是drawable類的對象) * 1.當(dāng)EditText文本內(nèi)容為空的時候,右側(cè)清空圖標(biāo)應(yīng)為灰色,此時點擊是沒有任何效果的 * 2.當(dāng)EditText文本內(nèi)容不為空的時候,右側(cè)清空圖標(biāo)應(yīng)為藍色,此時點擊,清空EditText文本內(nèi)容 * 3.上下文對象 */ private Drawable imageBlue; private Drawable imageGray; private Context myContext; /** * 實現(xiàn)EditText父類的三個構(gòu)造方法 * 這三個方法必須調(diào)用自定義的初始化函數(shù) init()方法 */ public MyEditText(Context context) { super(context); init(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } /** * 初始化方法:用于初始化聲明的三個全局變量 :imageBlue,imageGray,myContext * 并負責(zé)監(jiān)聽EditText文本內(nèi)容的更改 */ private void init(Context context){ this.myContext = context; /** * 得到圖片資源: * 第一種方式:(已過時,不推薦使用, * 還應(yīng)注意R文件導(dǎo)入的包應(yīng)為自己項目下的包, * 因為圖片資源在自己項目目錄下): * imageBlue = myContext.getResources().getDrawable(R.drawable.delete); * * 第二種方式:(網(wǎng)友推薦,項目會報錯?) * 調(diào)用getDrawable()帶兩個參數(shù)的方法.第二參數(shù)置為null * imageBlue = myContext.getResources().getDrawable(R.drawable.delete, null); * * 第三種方式:(谷歌官方推薦使用) ,myContext為自己聲明的上下文對象 * imageBlue = ContextCompat.getDrawable(myContext, R.drawable.delete); */ imageBlue = ContextCompat.getDrawable(myContext, R.drawable.delete); imageGray = ContextCompat.getDrawable(myContext, R.drawable.delete_gray); /** * 設(shè)置文字監(jiān)聽器(EditText文本內(nèi)容改變時,會觸發(fā)對應(yīng)的回調(diào)函數(shù)) * onTextChanged() EditText文本內(nèi)容更改時觸發(fā) * beforeTextChanged() EditText文本內(nèi)容更改前觸發(fā) * afterTextChanged() EditText文本內(nèi)容更改后觸發(fā) * * 對于此項目,清空EditText應(yīng)在EditText文本內(nèi)容更改后觸發(fā) * */ addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { //文字改變后設(shè)置清空圖片的位置 setImage(); } }); //初始的時候也應(yīng)設(shè)置清空圖片的位置 setImage(); } /** * 設(shè)置圖片位置方法 * 當(dāng)length()大于0,即 EditText里面有文本內(nèi)容的時候,圖片為藍色 * 當(dāng) length()小于0,即 EditText里面沒有文本內(nèi)容的時候,圖片為灰色 * setCompoundDrawablesWithIntrinsicBounds() 四個參數(shù)代表左上右下 */ private void setImage(){ if (length()>0) { setCompoundDrawablesWithIntrinsicBounds(null,null,imageBlue,null); }else{ setCompoundDrawablesWithIntrinsicBounds(null, null, imageGray, null); } } public boolean onTouchEvent(MotionEvent event){ switch (event.getAction()){ //匹配手指離開EditText case MotionEvent.ACTION_UP: //得到手指離開EditText時的X Y坐標(biāo) int x = (int) event.getRawX(); int y = (int) event.getRawY(); //創(chuàng)建一個長方形 Rect rect = new Rect(); //讓長方形的寬等于edittext的寬,讓長方形的高等于edittext的高 getGlobalVisibleRect(rect); //把長方形縮短至右邊50個寬度內(nèi) rect.left = rect.right - 50; //如果x和y坐標(biāo)在長方形當(dāng)中,說明你點擊了右邊的xx圖片,清空輸入框 if(rect.contains(x,y)){ setText(""); } break; default: break; } return super.onTouchEvent(event); } }
自定義的MyEditText這個類實際跟我們在layout布局文件里面拖的EditText控件是一樣的,因為MyEditText繼承自父類EditText.只不過我們在layout布局文件里面調(diào)用的時候應(yīng)包括完整包名,如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="myself.myapplication.MainActivity"> <com.liheng.MyEditText android:layout_width="500dp" android:layout_height="50dp" android:inputType="textPersonName" android:text="Name" android:ems="10" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="45dp" android:layout_marginStart="45dp" android:layout_marginTop="49dp" android:id="@+id/editText" android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/select_edittext"/> <com.liheng.MyEditText android:layout_width="500dp" android:layout_height="50dp" android:inputType="textPersonName" android:text="Name" android:ems="10" android:layout_below="@+id/editText" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:layout_marginTop="89dp" android:id="@+id/editText2" android:paddingLeft="10dp" android:background="@drawable/select_edittext"/> </RelativeLayout>
附上實際效果圖:
仔細觀察可以發(fā)現(xiàn),第一個輸入框的清空圖標(biāo)往左靠了一點,這是因為我在第一個輸入框的xml布局文件里面加了 android:paddingRight=”10dp” 這一行代碼.如此一來,對于清空圖標(biāo)的位置,我們可以根據(jù)手機分辨率大小做具體調(diào)整.
圖標(biāo)素材:
以上所述是小編給大家介紹的Android Studio EditText點擊圖標(biāo)清除文本內(nèi)容,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android Studio default not found錯誤解決辦法
- 打造酷炫的AndroidStudio插件
- Android studio 出現(xiàn) Unsupported major.minor version 52.0解決辦法
- Android Studio Intent隱式啟動,發(fā)短信,撥號,打電話,訪問網(wǎng)頁等實例代碼
- 使用Android Studio檢測內(nèi)存泄露(LeakCanary)
- Android Studio gradle 編譯提示‘default not found’ 解決辦法
- Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the stack trace. Run with --info or --debu
- Ubuntu16.04 LTS 下安裝 Android Studio 2.2.2 的詳細步驟
- Android studio中生成引用.aar和.jar的方法詳解
- Android 詳解Studio引用Library與導(dǎo)入jar
相關(guān)文章
Android Intent 用法全面總結(jié)及實例代碼
這篇文章主要介紹了Android Intent 用法全面總結(jié)的相關(guān)資料,并附實例代碼,需要的朋友可以參考下2016-09-09Android實現(xiàn)搜索功能并本地保存搜索歷史記錄
這篇文章主要為大家詳細介紹了Android實現(xiàn)搜索功能,并實現(xiàn)本地保存搜索歷史記錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03Android 實現(xiàn)調(diào)用系統(tǒng)照相機拍照和錄像的功能
這篇文章主要介紹了Android 實現(xiàn)調(diào)用系統(tǒng)照相機拍照和錄像的功能的相關(guān)資料,需要的朋友可以參考下2016-11-11初學(xué)Android之網(wǎng)絡(luò)封裝實例
大家好,本篇文章主要講的是初學(xué)Android之網(wǎng)絡(luò)封裝實例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android端內(nèi)數(shù)據(jù)狀態(tài)同步方案VM-Mapping詳解
這篇文章主要介紹了Android端內(nèi)數(shù)據(jù)狀態(tài)同步方案VM-Mapping詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09android圖像繪制(四)自定義一個SurfaceView控件
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下2013-01-01