国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義EditText右側帶圖片控件

2019-12-12 04:57:16
字體:
來源:轉載
供稿:網友

前言

  最近項目做用戶登錄模塊需要一個右邊帶圖片的EditText,圖片可以設置點擊效果,所以就查資料做了一個自定義EditText出來,方便以后復用。

原理

  下面是自定義EditText的代碼,具體難點是要實現圖片的點擊監聽,因為谷歌官方至今沒有給出一個直接實現EditText里面圖片的監聽API。我的做法是整個控件綁定一個OnTouchListener,然后監測點擊事件,檢測點擊位置的X坐標是否在圖片的覆蓋范圍內(下面getCompoundDrawables()[2]里面的2是代表圖片在EditText的右邊),如果是則執行點擊事件。

package scut.userlogin;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.EditText;/** * Created by yany on 2016/7/23. */public class EditText_PassWordDisplay extends EditText implements View.OnTouchListener { //需要實現下面的幾個構造函數,不然有可能加載不了這個EditText控件 public EditText_PassWordDisplay(Context context) { super(context); init(); } public EditText_PassWordDisplay(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public EditText_PassWordDisplay(Context context, AttributeSet attrs) { super(context, attrs); init(); } //初始化控件,綁定監聽器 public void init(){ setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { //如果不是按下操作,就不做處理,如果是按下操作但是沒有圖片,也不做處理 if (event.getAction() == MotionEvent.ACTION_UP && this.getCompoundDrawables()[2] != null) {  //檢測點擊區域的X坐標是否在圖片范圍內  if (event.getX() > this.getWidth()   - this.getPaddingRight()   - this.getCompoundDrawables()[2].getIntrinsicWidth()) {  //在此做圖片的點擊處理  System.out.println("點擊區域");  MessageShow.ShowToast(getContext(), "點擊了圖片");  }  return false; } return false; }}

只需要在xml里使用這個控件(記得加上圖片,不然的話就相當于一個普通的EditText了):

<?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: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="scut.userlogin.RegisterActivity3"> <scut.userlogin.EditText_PassWordDisplay android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/EditText_PasswordRegisterInput" android:inputType="textPassword" android:hint="請輸入登錄密碼" android:drawableRight="@mipmap/ic_launcher" android:layout_marginTop="50dp" /></RelativeLayout>

在Activity里只需要普通地加載就行了:

 private EditText_PassWordDisplay et_PasswordRegisterInput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register3); init(); } private void init(){ et_PasswordRegisterInput = (EditText_PassWordDisplay) findViewById(R.id.EditText_PasswordRegisterInput); }

實現效果,點擊圖片就會出現Toast:

參考文章:

Android中EditText的drawableRight屬性設置點擊事件

Android對EditTex的圖片實現監聽

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 济阳县| 云和县| 郴州市| 上思县| 南京市| 含山县| 河津市| 砚山县| 伊金霍洛旗| 石泉县| 历史| 白城市| 西平县| 阿坝县| 凯里市| 潼关县| 大方县| 龙州县| 阜宁县| 高阳县| 博乐市| 永登县| 兰溪市| 英吉沙县| 邵武市| 安塞县| 迁西县| 民县| 昌都县| 绍兴县| 涡阳县| 天长市| 宾阳县| 连南| 普陀区| 东港市| 乌鲁木齐县| 和田市| 社旗县| 绥棱县| 巴青县|