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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android widget之CompoundButton

發布時間:2024/8/1 Android 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android widget之CompoundButton 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

具有兩個狀態的按鈕,已選中或未選中。當按下或點擊按鈕時,狀態會自動更改。

  • 直接繼承至Button
  • 直接子類
  • CheckBox
  • RadioButton
  • Switch
  • SwitchCompat
  • ToggleButton
  • 間接子類
  • AppCompatCheckBox
  • AppCompatRadioButton

使用

相比較Button而言多出了一個監聽事件(接口)

CompoundButton.OnCheckedChangeListener

當復合按鈕的檢查狀態發生變化時調用。

實現方法:onCheckedChanged( CompoundButton buttonView,boolean isChecked)

  • buttonView 復合按鈕視圖的狀態。
  • isChecked buttonView的新狀態。

公共方法

簡單介紹幾個常用的

  • isChecked() — 獲取當前狀態
  • performClick() — 調用此視圖的OnClickListener(如果已定義)
  • setChecked(boolean checked) — 更改這個按鈕的狀態
  • setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)
    當這個按鈕的檢查狀態發生變化時,注冊一個回調
  • toggle() — 將視圖的狀態更改為當前狀態的逆(反向)

子類

CheckBox

復選框:可以選中或取消選中的特定類型的雙狀態按鈕。

例:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><CheckBox android:id="@+id/checkbox_meat"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/meat"android:onClick="onCheckboxClicked"/><CheckBox android:id="@+id/checkbox_cheese"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/cheese"android:onClick="onCheckboxClicked"/> </LinearLayout> public void onCheckboxClicked(View view) {// Is the view now checked?boolean checked = ((CheckBox) view).isChecked();// Check which checkbox was clickedswitch(view.getId()) {case R.id.checkbox_meat:if (checked)// Put some meat on the sandwichelse// Remove the meatbreak;case R.id.checkbox_cheese:if (checked)// Cheese meelse// I'm lactose intolerantbreak;// TODO: Veggie sandwich} } public class MyActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.content_layout_id);final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);if (checkBox.isChecked()) {checkBox.setChecked(false);}}}

注:AppCompatCheckBox作為其子類用法差別不大!

RadioButton

單選按鈕:是可以選中或取消選中的雙狀態按鈕。當單選按鈕被取消選中時,用戶可以單擊來選中它。

  • 注:單選按鈕通常與RadioGroup在一起使用。當多個單選按鈕在RadioGroup內時,檢查一個單選按鈕將取消選中所有其他單選按鈕。
<?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButton android:id="@+id/radio_pirates"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/pirates"android:onClick="onRadioButtonClicked"/><RadioButton android:id="@+id/radio_ninjas"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/ninjas"android:onClick="onRadioButtonClicked"/> </RadioGroup> public void onRadioButtonClicked(View view) {// Is the button now checked?boolean checked = ((RadioButton) view).isChecked();// Check which radio button was clickedswitch(view.getId()) {case R.id.radio_pirates:if (checked)// Pirates are the bestbreak;case R.id.radio_ninjas:if (checked)// Ninjas rulebreak;} }

注:AppCompatRadioButton作為其子類用法差別不大!

Switch

開關:是一個雙狀態切換開關小部件,可以在兩個選項之間進行選擇。用戶可以來回拖動“拇指”來選擇所選擇的選項,或者只需輕按以切換,就像復選框一樣。該text 屬性控制交換機標簽中顯示的文本,而 文本off和on文本控制拇指上的文本。

xml屬性公共方法作用效果
android:showTextsetShowText(boolean)是否顯示 打開/關閉 文本
android:textOffsetTextOff(CharSequence)當開關處于 關閉 狀態時使用的文本
android:textOnsetTextOn(CharSequence)當開關在 開打 狀態時使用的文本
android:tracksetTrackResource(int)開關拇指滑動的“軌跡”

ToggleButton

顯示 打開/關閉 的狀態的按鈕,默認情況下伴隨文本“ON”或“OFF”。

與Switch差別不大!



知識不夠用?

http://android.xsoftlab.net/reference/android/widget/CompoundButton.html


知識貴在分享!

總結

以上是生活随笔為你收集整理的Android widget之CompoundButton的全部內容,希望文章能夠幫你解決所遇到的問題。

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