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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android CheckBox 修改选择框

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

效果圖

是否被選中打印效果圖圖

?

CheckBox? 是多選框 ,選中之后可以再次點擊取消操作.

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

下面第一個位置調整了 使用了paddingleft?

在androidx以前布局

    <CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="同意本協議" />

在androidx 中布局使用? 使用(不過使用CheckBox還是可以的)

    <androidx.appcompat.widget.AppCompatCheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="10dp"android:text="同意本協議" />

下面說想修改前面的圖片?

CheckBox 修改選擇框的圖片 可以通過使用style 或者直接是不button 來修改

?1 通過 style?

xml 代碼如下

    <CheckBoxandroid:id="@+id/checkbox"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="同意本協議"style="@style/checkbox"/>

?values 里面 styles 代碼如下

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

checkbox 使用selector如下

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

這樣修改好了,

2 直接使用button

 <CheckBoxandroid:id="@+id/checkbox"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="同意本協議"android:button="@drawable/checkbox"/>
checkbox 的代碼
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/check_unselect" android:state_checked="false" /><item android:drawable="@drawable/checkselect" android:state_checked="true" />
</selector>

關于CheckBox 是否被選中的監聽 使用setOnCheckedChangeListener 如下

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

做多選篩選的時候使用也是比較方便的

下面多一個多選題效果圖如下

代碼

  <CheckBoxandroid:id="@+id/checkbox11"android:layout_marginLeft="17dp"android:layout_marginRight="17dp"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center"android:button="@null"android:background="@drawable/drawable_radio_bg"android:text="選舉權和被選舉權" /><CheckBoxandroid:id="@+id/checkbox22"android:layout_marginLeft="17dp"android:layout_marginRight="17dp"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center"android:button="@null"android:background="@drawable/drawable_radio_bg"android:text="勞動權" /><CheckBoxandroid:id="@+id/checkbox33"android:layout_marginTop="10dp"android:layout_marginLeft="17dp"android:layout_marginRight="17dp"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center"android:button="@null"android:background="@drawable/drawable_radio_bg"android:text="受教育權" /><CheckBoxandroid:id="@+id/checkbox44"android:layout_marginLeft="17dp"android:layout_marginRight="17dp"android:layout_width="match_parent"android:layout_marginTop="10dp"android:layout_height="40dp"android:gravity="center"android:button="@null"android:background="@drawable/drawable_radio_bg"android:text="監督權" />

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>

然后就是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>

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 參考地址

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。