夜还不算太深,不知道有多少猴子们还坐在电脑面前搬砖,相信坚持下去会有收获的
做对话框的时候发现自己脑子一片空白,不知从何做起,就查阅了下:
单选框是RadioGroup 下面随意布置radiobutton
不过要改变原来的界面显示和选中颜色,需要状态选择器,
选择器代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 填充 --> <solid android:color="@color/blue" /> <!-- 圆角 --> <corners android:radius="5dp" /> </shape>如果不知道怎么创建shape为根节点的xml文件,就先创建一个selector,然后上面的 shape全部覆盖过去也是可以的。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/radiobutton_background_unchecked" android:state_checked="false" /> <item android:drawable="@drawable/radiobutton_background_checked" android:state_checked="true" /> </selector>
至于另一个选择器,是没有选中时的样子,你可以创建一个空的选择器就行。
下面是布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/rg" android:orientation="horizontal"> <RadioButton android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/radiobutton_background" android:button="@null" android:gravity="center" android:text="P0501" android:textColor="@color/radiobutton_textcolor" android:textSize="14sp" /> <RadioButton android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="35dp" android:layout_marginStart="10dp" android:layout_weight="1" android:background="@drawable/radiobutton_background" android:button="@null" android:gravity="center" android:text="P0502" android:textColor="@color/radiobutton_textcolor" android:textSize="14sp" /> <RadioButton android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="35dp" android:layout_marginStart="10dp" android:layout_weight="1" android:background="@drawable/radiobutton_background" android:button="@null" android:gravity="center" android:text="P0503" android:textColor="@color/radiobutton_textcolor" android:textSize="14sp" /> </RadioGroup> </LinearLayout>
在调用的时候的代码:
我这里只是将选中的那个按钮的名字土司出来,拿它做什么大家都应该知道的,毕竟我们的目的就是它
RadioGroup rg = (RadioGroup) findViewById(R.id.rg); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { RadioButton rb = (RadioButton) findViewById(checkedId); Toast.makeText(MainActivity.this,"dianji"+rb.getText(),Toast.LENGTH_SHORT).show(); } });
在布局中,
android:button="@null"
这句的作用就是不显示系统自带的那种难看的UI界面。
希望有干货的朋友们都分享下,这样我们才能一起越走越远!