多个View点击切换选中未选中

效果
在这里插入图片描述
步骤如下:
1.推荐单独创建一个布局文件,便于修改

layout_bet_select_input.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dimen_50"
    android:background="@color/white"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv1_default_money"
        style="@style/bet_select_input"
        android:text="10" />

    <TextView
        android:id="@+id/tv2_default_money"
        style="@style/bet_select_input"
        android:text="50" />

    <TextView
        android:id="@+id/tv3_default_money"
        style="@style/bet_select_input"
        android:text="100" />

    <TextView
        android:id="@+id/tv4_default_money"
        style="@style/bet_select_input"
        android:text="200" />

    <TextView
        android:id="@+id/tv5_default_money"
        style="@style/bet_select_input"
        android:text="500" />

    <TextView
        android:id="@+id/tv_editor"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.5"
        android:gravity="center"
        android:text="编辑"
        android:textColor="@color/deep_blue"
        android:textSize="@dimen/sp_18" />

</LinearLayout>

2.View引用的style,每个View的样式,根据自己需求调整

<style name="bet_select_input">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">@dimen/dimen_30</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:layout_marginStart">@dimen/dimen_8</item>
        <item name="android:background">@drawable/bet_select_input_bg_color</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">@drawable/bet_select_input_tv_color</item>
        <item name="android:textSize">@dimen/sp_16</item>
    </style>

3.View背景状态 background

bet_select_input_bg_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bet_select_input_true" android:state_selected="true" />
    <item android:drawable="@drawable/bet_select_input_false" />
</selector>

选中设置

bet_select_input_true.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="@dimen/dimen_50" />
    <stroke
        android:width="1dp"
        android:color="@color/deep_blue" />

</shape>

未选中设置

bet_select_input_false.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="@dimen/dimen_40" />
    <stroke
        android:width="1dp"
        android:color="@color/color_9E9E9E" />

</shape>

4.View文字状态 textColor

bet_select_input_tv_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/deep_blue" android:state_selected="true" />
    <item android:color="@color/color_9E9E9E" />
</selector>

5.需要的布局文件中导入

<include layout="@layout/layout_bet_select_input" />

6.对应Activity中添加代码

    @BindView(R.id.tv1_default_money)
    TextView tv1_default_money;
    @BindView(R.id.tv2_default_money)
    TextView tv2_default_money;
    @BindView(R.id.tv3_default_money)
    TextView tv3_default_money;
    @BindView(R.id.tv4_default_money)
    TextView tv4_default_money;
    @BindView(R.id.tv5_default_money)
    TextView tv5_default_money;
    @BindView(R.id.tv_editor)
    
    private View[] mllViews;

//保存这些view到数组
mllViews = new View[]{tv1_default_money, tv2_default_money, tv3_default_money, tv4_default_money, tv5_default_money};

//设置点击事件
@OnClick({R.id.tv1_default_money,R.id.tv2_default_money,R.id.tv3_default_money,R.id.tv4_default_money,R.id.tv5_default_mone})
    public void onViewClicked(View view) {
        switch (view.getId()) { 
            case R.id.tv1_default_money:
                changeSelectState(0);
                break;
            case R.id.tv2_default_money:
                changeSelectState(1);
                break;
            case R.id.tv3_default_money:
                changeSelectState(2);
                break;
            case R.id.tv4_default_money:
                changeSelectState(3);
                break;
            case R.id.tv5_default_money:
                changeSelectState(4);
                break;
        }
    }

//修改选中和未选中状态变化
private void changeSelectState(int index) {
        for (int i = 0; i < mllViews.length; i++) {
            mllViews[i].setSelected(index == i);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值