Android中控件的事件——xml设置对应效果state_[pressed,checked,selected,focused]

本篇文章主要是讨论下控件的点击事件

一、在Drawable中添加控件的backgroundColor样式(点击切换颜色)

直接贴测试结果了:【在Drawable中创建xml资源文件,在其中的Selector标签对中添加样式(如下黑色截图)】

Android中控件的事件——xml设置对应效果state_[pressed,checked,selected,focused]_第1张图片

ImageButton

ImageView

Android中控件的事件——xml设置对应效果state_[pressed,checked,selected,focused]_第2张图片

调用方式:check是Drawable中的资源文件名

 
二、在xml中改变textView的点击样式(按下时textColor变色)【不能用于TextView的background,会报错】

1.调用代码和xml代码(Drawable中的资源文件)如下

Android中控件的事件——xml设置对应效果state_[pressed,checked,selected,focused]_第3张图片         Android中控件的事件——xml设置对应效果state_[pressed,checked,selected,focused]_第4张图片

设置监听事件:textView.setOnClickListener(null);【不设置不生效】

效果:按在textView上文字的颜色变为Orange

你可能感兴趣的:(Android基础)