日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android RadioButton 修改选择框

發(fā)布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android RadioButton 修改选择框 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

效果圖

?是否被選中打印效果圖

RadioButon? 是單選框 ,選中之后再次點擊無法取消,這樣呢我們可以配合RadioGroup 使用,選擇其他的RadioButton

這樣的話剛才的的那個就取消了

在使用的時候注意padding 是相對于字體來的不是相對選擇框?

在androidx一起布局使用

RadioButton

androidx 可以使用 (RadioButton 依然可以使用)

androidx.appcompat.widget.AppCompatRadioButton

修改選擇框的圖片 和checkbox 一樣,可以使用style 還可以直接通過button 來實現(xiàn)

1 style?

xml 代碼如下

    <androidx.appcompat.widget.AppCompatRadioButtonandroid:id="@+id/radio"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="10dp"style="@style/checkbox"android:text="Java"/>

styles 里面 checkbox

    <style name="checkbox"><item name="android:button">@drawable/checkbox</item></style>

drawable 里面的 checkbox (注意這里的checkbox_unselect 與checkbox_select是圖片)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/checkbox_unselect" android:state_checked="false" /><item android:drawable="@drawable/checkbox_select" android:state_checked="true" />
</selector>

這樣就實現(xiàn)了修改圖片的效果圖

直接button xml 代碼

  <androidx.appcompat.widget.AppCompatRadioButtonandroid:id="@+id/radio"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="10dp"android:button="@drawable/drawable_radio"android:text="Java"/>

drawable 下面的 drawable_radio (注意:這里的radio_select與 radio_unselect是圖片)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/radio_unselect" android:state_checked="false" /><item android:drawable="@drawable/radio_select" android:state_checked="true" />
</selector>

關(guān)于RadioButton 是否選中的監(jiān)聽事件setOnCheckedChangeListener

代碼

public class MainActivity extends AppCompatActivity {private RadioButton radioButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioButton = findViewById(R.id.radio);radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {Log.e("--------java是否被選中", String.valueOf(isChecked));}});}
}

有時候做篩選的時候可能使用到?

下面寫一個單選題把

效果圖

這個就是簡單的修改了下直接看代碼把

 <TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="發(fā)射第一顆人造衛(wèi)星的國家是"/><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="17dp"android:layout_marginRight="17dp"android:orientation="vertical"><RadioButtonandroid:layout_height="40dp"android:layout_width="match_parent"android:button="@null"android:layout_marginTop="10dp"android:text="美國"android:gravity="center"android:background="@drawable/drawable_radio_bg"/><RadioButtonandroid:layout_height="40dp"android:layout_width="match_parent"android:button="@null"android:text="前蘇聯(lián)"android:layout_marginTop="10dp"android:gravity="center"android:background="@drawable/drawable_radio_bg"/><RadioButtonandroid:layout_height="40dp"android:layout_width="match_parent"android:button="@null"android:text="南斯拉夫"android:layout_marginTop="10dp"android:gravity="center"android:background="@drawable/drawable_radio_bg"/><RadioButtonandroid:layout_height="40dp"android:layout_width="match_parent"android:button="@null"android:layout_marginTop="10dp"android:text="中國"android:gravity="center"android:background="@drawable/drawable_radio_bg"/></RadioGroup>

drawable 中的 drawable_radio_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/radio_bg" android:state_checked="false" /><item android:drawable="@drawable/radio_select_bg" android:state_checked="true" />
</selector>

drawable 中的 radio_bg?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="10dp"/><stroke android:color="#1296db" android:width="1dp"/>
</shape>

drawable 中的 radio_select_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="10dp"/><solid android:color="#1296db"/>
</shape>

demo 地址參考

總結(jié)

以上是生活随笔為你收集整理的Android RadioButton 修改选择框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。