Android中的Selector

selector的用法

android中selector主要用于在不同的状态下设置不同的背景或者不同的颜色。
selector分为两种类型,colorselector和drawableselector。colorselector用来在不同的状态下设置不同的颜色,而drawableselector用于在不同的状态下设置不同的背景。
selector文件中item的常见的状态主要有以下几种

状态意义
android:state_selected被选择时的状态
android:state_focused获得焦点时的状态
android:state_pressed被按压时的状态
android:state_enabled控件能否处理touch或者click事件时的状态
android:state_active激活状态,API11及以上才支持,可通过代码调用控件的setActivated(boolean)方法设置是否激活该控件
android:state_checkable是否可以被checked的状态,只有像单选按钮、多选按钮的控件此状态才有效
android:state_checked是否被选中时的状态,也只有在类似单选按钮、多选按钮这样的控件才有效
android:state_hovered当光标移动到某一个控件时的状态
android:state_window_focused当前界面是否得到焦点的状态

特别提醒:在匹配的时候,是从上往下匹配的,如果匹配到一个item就采用这个item,所以默认的状态要写在最后,防止后面的item没有被匹配。

下面是一个简单的例子:
MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = MainActivity.class.getSimpleName();

    private EditText et1;
    private EditText et2;
    private TextView tv;
    private Button mBtn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        tv = findViewById(R.id.tv);
        //强制TextView获取焦点
        tv.requestFocus();

        et1 = findViewById(R.id.et1);
   
        mBtn1 = findViewById(R.id.btn);
        mBtn1.setOnClickListener(this);

        tv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d(TAG, "onFocusChange: " + hasFocus);
              
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                tv.requestFocus();
                break;
        }
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/ib"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中华人民共和国万岁"
        android:layout_centerInParent="true"
        android:background="@drawable/selector_imagebutton"
       />


    <TextView
        android:id="@+id/tv"
        android:layout_above="@id/ib"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="中华人民共和国万岁"
        android:textColor="@color/et_color"
        android:textSize="30sp" />

    <EditText
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/et1"
        android:layout_below="@id/ib"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:textColor="@color/et_color"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击我改变焦点的位置"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

et_color.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--设置的是EditText中文字的颜色-->
    <item android:color="@color/black" android:state_focused="false"/>
    <item android:color="@color/purple_200" android:state_focused="true"/>

</selector>

selector_imagebutton.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<!--当被按压时,背景设置为ic_launcher_foreground-->
    <item android:state_pressed="true" android:drawable="@drawable/ic_launcher_foreground"/>
    <!--当没有被按压时,背景设置为ic_launcher_background-->
    <item android:state_pressed="false" android:drawable="@drawable/ic_launcher_background"/>
    
</selector>

可以发现,点击底部的按钮时,中华人民共和国万岁这几个字的颜色会发生变化;按压图片按钮时,图片按钮的背景图片会发生改变;在输入框输入文字时字体的颜色和焦点离开时文字的颜色是不一样的。

参考

  1. Selector中的各种状态详解
  2. Android 02 selector状态选择器
  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android,`selector`是一个XML文件,用于定义不同状态下的视图样式。它可以应用于按钮、列表项、文本等视图元素,以根据不同的触摸或焦点状态显示不同的样式。 以下是使用`selector`属性的步骤: 1. 首先,在`res/drawable`目录下创建一个XML文件,例如`button_selector.xml`。 2. 在XML文件,使用`selector`元素作为根元素,并在其定义不同状态下的样式。例如,你可以定义按下状态(`state_pressed`)和默认状态(`state_default`)下的样式。 ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <item android:state_default="true" android:drawable="@drawable/button_default" /> </selector> ``` 在上面的示例,`button_pressed`和`button_default`是指向其他drawable资源的引用,可以是颜色、形状或图片等。 3. 然后,在你的布局文件(XML)使用这个`selector`作为按钮的背景属性。例如,你可以使用`android:background`属性来指定按钮的背景。 ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" android:background="@drawable/button_selector" /> ``` 现在,当按钮处于按下状态时,它将显示`button_pressed`的样式;否则,它将显示`button_default`的样式。 `selector`属性不仅可以应用于按钮,还可以应用于其他视图元素,如TextView、ImageView等。你可以根据需要在`selector`定义更多的状态和样式。 希望这可以帮助到你!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值