监听android:drawableLeft和android:drawableRight点击事件
Android官方没有提供对android:drawableLeft和android:drawableRight点击事件的监听,但有些情况下,如下面的搜索栏,
需要在用户输入字符点击左侧的搜索图标后触发搜索事件,而此搜索icon是通过android:drawableLeft添加的,此时就需要对android:drawableLeft上去的icon进行事件监听。我写了一个工具DrawableUtil类,对android:drawableLeft和android:drawableRight进行监听。
import android.graphics.drawable.Drawable; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; public class DrawableUtil { /** * TextView四周drawable的序号。 * 0 left, 1 top, 2 right, 3 bottom */ private final int LEFT = 0; private final int RIGHT = 2; private OnDrawableListener listener; private TextView mTextView; public DrawableUtil(TextView textView, OnDrawableListener l) { mTextView = textView; mTextView.setOnTouchListener(mOnTouchListener); listener = l; } public interface OnDrawableListener { public void onLeft(View v, Drawable left); public void onRight(View v, Drawable right); } private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: if (listener != null) { Drawable drawableLeft = mTextView.getCompoundDrawables()[LEFT]; if (drawableLeft != null && event.getRawX() <= (mTextView.getLeft() + drawableLeft.getBounds().width())) { listener.onLeft(v, drawableLeft); return true; } Drawable drawableRight = mTextView.getCompoundDrawables()[RIGHT]; if (drawableRight != null && event.getRawX() >= (mTextView.getRight() - drawableRight.getBounds().width())) { listener.onRight(v, drawableRight); return true; } } break; } return false; } }; }
使用DrawableUtil,在构造时候传递一个OnDrawableListener即可完成监听,如:
import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class DrawableActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawable_activity); TextView textView = findViewById(R.id.text); DrawableUtil drawableUtil = new DrawableUtil(textView, new DrawableUtil.OnDrawableListener() { @Override public void onLeft(View v, Drawable left) { Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_SHORT).show(); } @Override public void onRight(View v, Drawable right) { Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_SHORT).show(); } }); } }
注意要对xml布局中的TextView添加事件属性为true:
android:clickable="true"

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
IBM与西班牙桑坦德银行合作,推广企业级 iOS 银行应用
全球市值最高的银行之一, 西班牙桑坦德银行(Banco Santande)今天宣布,将与IBM合作,开发一系列原生 iOS 应用,完成公司业务的“数字变革”,这些全新企业级 iOS 应用将帮助公司员工更好的与客户交流。 目前,西班牙桑坦德银行商业网络员工中配备了1.1 万台 iOS 设备,未来新应用将部署在这些设备中。两家公司合作开发的首款应用是“成就预览”,可以帮助支行经理和地区监管机构监测最重要的成就指标。 西班牙桑坦德银行拥有 1.25亿顾客,12200 家分行和 188000 名员工。本月早些时候,西班牙桑坦德银行宣布在美国支持 Apple Pay,同时去年冬季,桑坦德银行也是首批支持西班牙 Apple Pay 的银行之一。 本文出处:畅享网 本文来自云栖社区合作伙伴畅享网,了解相关信息可以关注vsharing.com网站。
- 下一篇
动画
动画 facebook:rebound (A Java library that models spring dynamics and adds real world physics to your app) ViewAnimator (A fluent Android animation library) Material-Animations (Android Transition animations explanation with examples) AndroidViewAnimations (Cute view animation collection.) ListViewAnimations (An Android library which allows developers to easily add animations to ListView items) recyclerview-animators (An Android Animation library which easily add itemanimator to RecyclerView items...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS8安装Docker,最新的服务器搭配容器使用
- 设置Eclipse缩进为4个空格,增强代码规范
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- MySQL8.0.19开启GTID主从同步CentOS8