前段时间,添加客户性别选择按钮,原型如图:
然后通过书写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);
}