记录性别选择TextView,选中状态

这篇博客介绍了如何在Android应用中实现性别选择功能。通过XML布局文件创建性别选择按钮,使用Shape Drawable定义选中和未选中状态,并通过Java代码处理点击事件来切换选中状态。同时,使用Selector进行颜色和背景的动态改变,实现了视觉上的交互反馈。
摘要由CSDN通过智能技术生成

前段时间,添加客户性别选择按钮,原型如图:

然后通过书写background 和color 来展示是否选中:

具体代码如下:

xml:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_50"
        android:layout_marginTop="@dimen/dp_20"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingStart="@dimen/dp_15"
        android:paddingEnd="@dimen/dp_15"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="性别:"
            android:textColor="@color/color_333"
            android:textSize="@dimen/size_17" />

        <TextView
            android:id="@+id/tv_sex_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/dp_20"
            android:background="@drawable/bg_sex_textview_drawable"
            android:paddingStart="@dimen/dp_20"
            android:paddingTop="@dimen/dp_4"
            android:paddingEnd="@dimen/dp_20"
            android:paddingBottom="@dimen/dp_4"
            android:text="男"
            android:textColor="@color/sex_text_color"
            android:textSize="@dimen/size_15sp" />

        <TextView
            android:id="@+id/tv_sex_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_sex_textview_drawable"
            android:paddingStart="@dimen/dp_20"
            android:paddingTop="@dimen/dp_4"
            android:paddingEnd="@dimen/dp_20"
            android:paddingBottom="@dimen/dp_4"
            android:text="女"
            android:textColor="@color/sex_text_color"
            android:textSize="@dimen/size_15sp" />

    </LinearLayout>

背景 :drawable/bg_sex_textview_drawable.xml:

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

drawable/bg_sex_text_select.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="4dp" />
    <solid android:color="@color/colorPrimaryDark" />
</shape>

drawable/bg_sex_text_unselect.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/dp_4"/>
    <stroke android:color="@color/colorPrimaryDark"
        android:width="@dimen/dp_1"/>
</shape>

颜色:color/sex_text_color.xml

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

    <item android:color="@color/color_666" />
</selector>

java代码:

  @Override
    protected void initView(View view) {
        tvMan = findViewById(R.id.tv_sex_man);
        tvWoman =findViewById(R.id.tv_sex_woman);
        tvMan.setSelected(true);//设置默认选择
        tvMan.setOnClickListener(this);
        tvWoman.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sex_man:
                setSelectStatus(true, false);
                break;
            case R.id.tv_sex_woman:
                setSelectStatus(false, true);
                break;
        }
    }

    private void setSelectStatus(boolean b, boolean b1) {
        tvMan.setSelected(b);
        tvWoman.setSelected(b1);
    }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值