1. Android 自定義 CheckBox 文字與圖標的方向
android:button="@null"
android:drawableRight="@drawable/button_img"
button_img是checkBox的Button圖片 圖片也可以用系統的
加上這兩個就可以實現你的要求了
2. 安卓checkbox中的按鈕大小
protected void Page_Load(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
chk.Text = "testall"; // 這里可以換成資料庫的內容
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
chk.AutoPostBack = true;
Page.Form.Controls.Add(chk);
for (int i = 0; i < 10; i++)
{
CheckBox chk2 = new CheckBox();
chk2.Text = "test" + i.ToString(); // 這里可以換成資料庫的內容
chk2.Checked = (i % 3 == 0); // 這里可以換成資料庫的內容
Page.Form.Controls.Add(chk2);
}
}
void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
foreach(Control ctl in Page.Form.Controls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
chk.Checked = all.Checked;
}
}
}
3. Android中的checkBOX在代碼中怎麼設置至少選中一個
CheckBox和Button一樣,是android系統提供的最原始的控制項,它的優點在於,不用用戶去填寫具體的信息,只需輕輕點擊,缺點在於只有「是」和「否」兩種情況,但我們往往利用它的這個特性,來獲取用戶的一些信息。
1.CheckBox的常用屬性
checked屬性是CheckBox最重要的屬性之一,改變方式有兩種,xml中定義
android:checked="true|false"
表示選中和不選中
2.在代碼中設置選擇狀態
checkBox.setChecked(true|false);
3.獲取CheckBox的狀態
checkBox.isChecked();
true表示選中,false表示未選中
4.checkBox的應用
1.如果不確定某一組選項有幾個的時候,例如多選之前刪除,那麼要使用listView
+
adapter
其中checkBox存放在listView的adapter中,代碼實現比較復雜,需要自己去注冊checkBox的事件
2.如果checkBox的選項是已經知的,例如興趣愛好,已知有多少個選項的情況下,那麼你只需要用個線性布局做為容器,將checkBox都放到這個容器中
3.獲取選中的文本
如果是用listView的話,只需要自己在adapter中寫一個方法,返回選中的數據即可得到文本,如果是其它容器做的話,只能去遍歷這個布局下的所有checkBox
然後調用
if(checkBox.isChecked()){
checkBox.getText().toString();
//即可得到選中的文本
}
有關checkBox的更多用法,建議查看官網API文檔
4. android 如何改變checkbox樣式
1、首先res/drawable中定義編寫如下樣式:
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@drawable/check_true" android:state_checked="true"></item>
<item android:drawable="@drawable/check_true" android:state_selected="true"></item>
<item android:drawable="@drawable/check_true" android:state_pressed="true"></item>
<item android:drawable="@drawable/check_false"></item>
</selector>
2、在layout中添加checkbox控制項
<CheckBox
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_check"
android:button="@null"
android:checked="true"/>
其中drwable/btn_check為1中頂一個文件名稱,另外必須將android:button設置為@null。
@drawable/check_true和@drawable/check_false為checkbox不同狀態的圖片,可自行設計。