如何在Android项目中使用selector改变按钮状态

2023-06-07,,

如何在Android项目中使用selector改变按钮状态?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

Android 使用selector改变按钮状态实例详解

在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置 :

state_pressed,state_checked,state_pressed,state_selected,state_focused,state_enabled 等几个状态:

< selector xmlns:android="http://schemas.android.com/apk/res/android">

< item android:state_pressed="true" android:drawable="@drawable/arrow_pressed" />

< item android:drawable="@drawable/arrow_normal" />

< item android:state_checked="true" android:drawable="@drawable/arrow_pressed" />

< /selector>

 其中的android:src="@drawable/imageselector"

< ImageView 
android:layout_width="wrap_content"
android:layout_height="@android:dimen/app_icon_size"
android:layout_alignParentRight="true" 
android:scaleType="fitCenter"
android:layout_gravity="center" 
android:src="@drawable/imageselector"
android:clickable="true" 
android:focusable="true" 
android:id="@+id/blacklistImageView"
android:layout_marginRight="10dip" />

 如果当触发的控件不是ImageView,而是别的控件,可在代码中用

blacklistImageView.setPressed(true);
 
blacklistImageView.setChecked(true);

看完上述内容,你们掌握如何在Android项目中使用selector改变按钮状态的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注本站行业资讯频道,感谢各位的阅读!

《如何在Android项目中使用selector改变按钮状态.doc》

下载本文的Word格式文档,以方便收藏与打印。