当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义EditText右侧带图片控件

Android自定义EditText右侧带图片控件

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

  最近项目做用户登录模块需要一个右边带图片的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的图片实现监听

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网