一、使用
/**
* 动态设置 点击事件 selector 的工具类 可以从本地添加 也可以从网络添加
*/
public class SelectorUtil {
/**
* 从 drawable 获取图片 id 给 Imageview 添加 selector
* @param context 调用方法的 Activity
* @param idNormal 默认图片的 id
* @param idPress 点击图片的 id
* @param iv 点击的 view
*/
public static void addSelectorFromDrawable(Context context , int idNormal, int idPress,ImageView iv){
StateListDrawable drawable = new StateListDrawable();
Drawable normal = context.getResources().getDrawable(idNormal);
Drawable press = context.getResources().getDrawable(idPress);
drawable.addState(new int[]{android.R.attr.state_pressed},press);
drawable.addState(new int[]{-android.R.attr.state_pressed},normal);
iv.setBackgroundDrawable(drawable);
}
/**
* 从 drawable 获取图片 id 给 Button 添加 selector
* @param context 调用方法的 Activity
* @param idNormal 默认图片的 id
* @param idPress 点击图片的 id
* @param button 点击的 view
*/
public static void addSelectorFromDrawable(Context context , int idNormal, int idPress,Button button){
StateListDrawable drawable = new StateListDrawable();
Drawable normal = context.getResources().getDrawable(idNormal);
Drawable press = context.getResources().getDrawable(idPress);
drawable.addState(new int[]{android.R.attr.state_pressed},press);
drawable.addState(new int[]{-android.R.attr.state_pressed},normal);
button.setBackgroundDrawable(drawable);
}
/**
* 从网络获取图片 给 ImageView 设置 selector
* @param clazz 调用方法的类
* @param normalUrl 获取默认图片的链接
* @param pressUrl 获取点击图片的链接
* @param imageView 点击的 view
*/
public static void addSeletorFromNet(final Class clazz, final String normalUrl, final String pressUrl, final ImageView imageView){
new AsyncTask<Void,Void,Drawable>(){
@Override
protected Drawable doInBackground(Void... params) {
StateListDrawable drawable = new StateListDrawable();
Drawable normal = loadImageFromNet(clazz,normalUrl);
Drawable press = loadImageFromNet(clazz, pressUrl);
drawable.addState(new int[]{android.R.attr.state_pressed},press);
drawable.addState(new int[]{-android.R.attr.state_pressed},normal);
return drawable;
}
@Override
protected void onPostExecute(Drawable drawable) {
super.onPostExecute(drawable);
imageView.setBackgroundDrawable(drawable);
}
}.execute();
}
/**
*
* 从网络获取图片 给 Button 设置 selector
* @param clazz 调用方法的类
* @param normalUrl 获取默认图片的链接
* @param pressUrl 获取点击图片的链接
* @param button 点击的 view
*/
public static void addSeletorFromNet(final Class clazz, final String normalUrl, final String pressUrl, final Button button){
new AsyncTask<Void,Void,Drawable>(){
@Override
protected Drawable doInBackground(Void... params) {
StateListDrawable drawable = new StateListDrawable();
Drawable normal = loadImageFromNet(clazz,normalUrl);
Drawable press = loadImageFromNet(clazz, pressUrl);
drawable.addState(new int[]{android.R.attr.state_pressed},press);
drawable.addState(new int[]{-android.R.attr.state_pressed},normal);
return drawable;
}
@Override
protected void onPostExecute(Drawable drawable) {
super.onPostExecute(drawable);
button.setBackgroundDrawable(drawable);
}
}.execute();
}
/**
* 从网络获取图片
* @param clazz 调用方法的类
* @param netUrl 获取图片的链接
* @return 返回一个 drawable 类型的图片
*/
private static Drawable loadImageFromNet(Class clazz, String netUrl) {
Drawable drawable =null;
try {
drawable = Drawable.createFromStream(new URL(netUrl).openStream(), "netUrl.jpg");
} catch (IOException e) {
MyLog.e(clazz.getName(),e.getMessage());
}
return drawable;
}
}
二、注意事项
1、设置 bankgrpud的时候,我们的selector状态选择器存放在res - drawable
内
2、设置TextColor属性的时候,我们的selector状态选择器存放在res - color
内
三、状态设置类型
//设置是否按压状态,一般在true时设置该属性,表示已按压状态,默认为false
android:state_pressed
//设置是否选中状态,true表示已选中,false表示未选中
android:state_selected
//设置是否勾选状态,主要用于CheckBox和RadioButton,true表示已被勾选,false表示未被勾选
android:state_checked
//设置勾选是否可用状态,类似state_enabled,只是state_enabled会影响触摸或点击事件,state_checkable影响勾选事件
android:state_checkable
//设置是否获得焦点状态,true表示获得焦点,默认为false,表示未获得焦点
android:state_focused
//设置触摸或点击事件是否可用状态,一般只在false时设置该属性,表示不可用状态
android:state_enabled
四、常用类型示例:
1、点击改变字体颜色 - android:state_pressed(按压状态)
selector状态选择器(bg_btn_one (存放 res - color))
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_pressed="true"/>
<item android:color="@color/colorAccent" />
</selector>
使用:
<Button
android:textColor="@color/bg_btn_one"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="样式一:点击改变字体颜色"
android:gravity="center"
/>
2、点击改变背景颜色
selector状态选择器(bg_btn_two (存放 res - drawable))
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorAccent" android:state_pressed="true"/>
<item android:drawable="@color/colorPrimary" />
</selector>
使用:
<Button
android:background="@drawable/bg_btn_two"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="样式二:点击改变背景颜色"
android:gravity="center"
/>
3、改变背景色同时改变字体色
selector状态选择器(bg_btn_one (存放 res - color))
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_pressed="true"/>
<item android:color="@color/colorAccent" />
</selector>
selector状态选择器(bg_btn_two (存放 res - drawable))
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorAccent" android:state_pressed="true"/>
<item android:drawable="@color/colorPrimary" />
</selector>
使用:
<Button
android:background="@drawable/bg_btn_two"
android:textColor="@color/bg_btn_one"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="样式三:改变背景色同时改变字体色"
android:gravity="center"
/>
4、android:state_checked (勾选状态)
selector状态选择器(bg_check_three(存放 res - drawable))
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimary" android:state_checked="true"/>
<item android:drawable="@color/colorAccent" />
</selector>
使用:
<CheckBox
android:layout_marginTop="20dp"
android:id="@+id/btn"
android:background="@drawable/bg_check_three"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="Checked:点击后背景状态长存"
android:gravity="center"
/>
5、android:state_focused (焦点状态)
selector状态选择器(bg_check_three(存放 res - color))
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_focused="true"/>
<item android:color="@color/colorAccent" />
</selector>
使用:
<EditText
android:textColor="@color/bg_edittext_one"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="焦点样式:改变背景色同时改变字体色"
android:gravity="center"
/>