最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

原理

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

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 {

 //需要實(shí)現(xiàn)下面的幾個(gè)構(gòu)造函數(shù),不然有可能加載不了這個(gè)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) {
  //檢測點(diǎn)擊區(qū)域的X坐標(biāo)是否在圖片范圍內(nèi)
  if (event.getX() > this.getWidth()
   - this.getPaddingRight()
   - this.getCompoundDrawables()[2].getIntrinsicWidth()) {

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

  }
  return false;
 }
 return false;
 }
}

只需要在xml里使用這個(gè)控件(記得加上圖片,不然的話就相當(dāng)于一個(gè)普通的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="請(qǐng)輸入登錄密碼"
 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);

 }

實(shí)現(xiàn)效果,點(diǎn)擊圖片就會(huì)出現(xiàn)Toast:

參考文章:

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

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

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

相關(guān)文章

  • Android開發(fā)自定義TextView省略號(hào)樣式的方法

    Android開發(fā)自定義TextView省略號(hào)樣式的方法

    這篇文章主要介紹了Android開發(fā)自定義TextView省略號(hào)樣式的方法,結(jié)合實(shí)例形式分析了Android文本控件TextView相關(guān)屬性與字符串操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android大作業(yè)功能設(shè)計(jì)之自動(dòng)登錄和記住密碼

    Android大作業(yè)功能設(shè)計(jì)之自動(dòng)登錄和記住密碼

    SharedPreferences是Android平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,主要是保存一些常用的配置參數(shù),它是采用xml文件存放數(shù)據(jù)的,文件存放在"/data/data<package?name>/shared_prefs"目錄下,由于SharedPreferences是一個(gè)接口,而且在這個(gè)接口里沒有提供寫入數(shù)據(jù)和讀取數(shù)據(jù)的能力
    2023-01-01
  • 基于Android SQLiteOpenHelper && CRUD 的使用

    基于Android SQLiteOpenHelper && CRUD 的使用

    本篇文章小編為大家介紹,基于Android SQLiteOpenHelper && CRUD的使用。需要的朋友可以參考一下
    2013-04-04
  • Android使用DocumentFile讀寫外置存儲(chǔ)的問題

    Android使用DocumentFile讀寫外置存儲(chǔ)的問題

    大家好,本篇文章主要講的是Android使用DocumentFile讀寫外置存儲(chǔ)的問題,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2021-12-12
  • Android使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)的方法

    Android使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)的方法

    這篇文章主要介紹了Android使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)的方法,較為詳細(xì)的分析了Android基于文件實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)所涉及的相關(guān)概念與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • 最新評(píng)論

    固镇县| 曲靖市| 宁城县| 小金县| 若羌县| 古蔺县| 团风县| 柏乡县| 安乡县| 读书| 宁都县| 新竹县| 丰县| 云浮市| 土默特右旗| 赤水市| 陕西省| 彰武县| 元氏县| 九龙城区| 东平县| 高唐县| 龙游县| 石嘴山市| 邯郸市| 刚察县| 新乡县| 南平市| 大同市| 合川市| 含山县| 深水埗区| 云阳县| 巨鹿县| 德江县| 玉环县| 大方县| 台东市| 咸丰县| 保亭| 库伦旗|