亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義EditText右側(cè)帶圖片控件

 更新時間:2016年10月26日 09:25:47   作者:炎之鎧  
這篇文章主要為大家詳細(xì)介紹了Android自定義EditText右側(cè)帶圖片控件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

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

原理

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

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 {

 //需要實現(xiàn)下面的幾個構(gòu)造函數(shù),不然有可能加載不了這個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();
 }

 //初始化控件,綁定監(jiān)聽器
 public void init(){
 setOnTouchListener(this);
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
 //如果不是按下操作,就不做處理,如果是按下操作但是沒有圖片,也不做處理
 if (event.getAction() == MotionEvent.ACTION_UP && this.getCompoundDrawables()[2] != null) {
  //檢測點擊區(qū)域的X坐標(biāo)是否在圖片范圍內(nèi)
  if (event.getX() > this.getWidth()
   - this.getPaddingRight()
   - this.getCompoundDrawables()[2].getIntrinsicWidth()) {

  //在此做圖片的點擊處理
  System.out.println("點擊區(qū)域");
  MessageShow.ShowToast(getContext(), "點擊了圖片");

  }
  return false;
 }
 return false;
 }
}

只需要在xml里使用這個控件(記得加上圖片,不然的話就相當(dāng)于一個普通的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);

 }

實現(xiàn)效果,點擊圖片就會出現(xiàn)Toast:

參考文章:

Android中EditText的drawableRight屬性設(shè)置點擊事件

Android對EditTex的圖片實現(xiàn)監(jiān)聽

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android封裝對原生Log進(jìn)行封裝的操作

    Android封裝對原生Log進(jìn)行封裝的操作

    這篇文章主要介紹了Android封裝對原生Log進(jìn)行封裝的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 仿墨跡天氣在Android App中實現(xiàn)自定義zip皮膚更換

    仿墨跡天氣在Android App中實現(xiàn)自定義zip皮膚更換

    這篇文章主要介紹了仿墨跡天氣在Android App中實現(xiàn)自定義zip皮膚更換的方法,即讓用戶可以自行通過自制或者下載的zip皮膚包進(jìn)行換膚,需要的朋友可以參考下
    2016-02-02
  • Android webview如何加載HTML,CSS等語言的示例

    Android webview如何加載HTML,CSS等語言的示例

    本篇文章主要介紹了Android webview如何加載HTML,CSS等語言的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android實現(xiàn)簡單的照相功能

    Android實現(xiàn)簡單的照相功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡單的照相功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android中實現(xiàn)GPS定位的簡單例子

    Android中實現(xiàn)GPS定位的簡單例子

    這篇文章主要介紹了Android中實現(xiàn)GPS定位的簡單例子,例子邏輯清晰,但相對簡單了些,需要的朋友可以參考下
    2014-07-07
  • Android實現(xiàn)短信發(fā)送功能

    Android實現(xiàn)短信發(fā)送功能

    這篇文章主要介紹了Android實現(xiàn)短信發(fā)送功能,對Android實現(xiàn)短信發(fā)送的每一步都進(jìn)行了詳細(xì)的介紹,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android自定義Toast之WindowManager

    Android自定義Toast之WindowManager

    這篇文章主要為大家詳細(xì)介紹了Android自定義Toast之WindowManager的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android中子線程和UI線程通信詳解

    Android中子線程和UI線程通信詳解

    這篇文章主要介紹了Android中子線程和UI線程通信詳解,本文講解了一些概念、使用、及代碼實例,需要的朋友可以參考下
    2015-06-06
  • 如何更改Dialog的標(biāo)題與按鈕顏色詳解

    如何更改Dialog的標(biāo)題與按鈕顏色詳解

    這篇文章主要給大家介紹了關(guān)于如何更改Dialog的標(biāo)題與按鈕顏色的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Android ListView與RecycleView的對比使用解析

    Android ListView與RecycleView的對比使用解析

    這篇文章主要介紹了Android ListView與RecycleView的對比使用解析,需要的朋友可以參考下
    2017-12-12

最新評論