0
A
回答
3
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#555555" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/options"
android:textSize="10pt"
android:typeface="serif"
android:textStyle="bold"
android:paddingTop="10dp"
android:paddingBottom="10dp"
/>
<CheckBox
android:id="@+id/blue_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif"
android:textStyle="bold"
android:text="@string/bluetooth"
/>
<CheckBox
android:id="@+id/wifi_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif"
android:textStyle="bold"
android:text="@string/wifi"
/>
<CheckBox
android:id="@+id/silent_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/silent"
android:typeface="serif"
android:textStyle="bold"
android:button="@drawable/box"/>
<CheckBox
android:id="@+id/general_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/general"
android:typeface="serif"
android:textStyle="bold"
android:button="@drawable/box"/>
<CheckBox
android:id="@+id/mobile_data_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mobile_data"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="80dp"
android:button="@drawable/switchbox"/>
<CheckBox
android:id="@+id/power_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/power_saver_mode"
android:typeface="serif"
android:textStyle="bold"
android:paddingLeft="80dp"
android:button="@drawable/switchbox"/>
</LinearLayout>
活動類:
package com.vimal.android.CustomCheckBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class CustomCheckBoxActivity extends Activity {
private CheckBox blueSwitch;
private CheckBox wifiSwitch;
private CheckBox silentSwitch;
private CheckBox generalSwitch;
private CheckBox mobileDataSwitch;
private CheckBox powerSwitch;
String msg="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
blueSwitch=(CheckBox) findViewById(R.id.blue_switch);
wifiSwitch=(CheckBox) findViewById(R.id.wifi_switch);
silentSwitch=(CheckBox) findViewById(R.id.silent_switch);
generalSwitch=(CheckBox) findViewById(R.id.general_switch);
mobileDataSwitch=(CheckBox) findViewById(R.id.mobile_data_switch);
powerSwitch=(CheckBox) findViewById(R.id.power_switch);
setListener();
}
private void setListener() {
blueSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
msg="BlueTooth is Switched OFF";
if(isChecked){
msg="BlueTooth is Switched ON";
}
Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();
}
});
wifiSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
msg="Wifi is Switched OFF";
if(isChecked){
msg="Wifi is Switched ON";
}
Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();
}
});
silentSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
msg="Silent Mode is Switched OFF";
if(isChecked){
msg="Silent Mode is Switched ON";
}
Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();
}
});
generalSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
msg="General Mode is Switched OFF";
if(isChecked){
msg="General Mode is Switched ON";
}
Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();
}
});
mobileDataSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
msg="Mobile Data is Switched OFF";
if(isChecked){
msg="Mobile Data is Switched ON";
}
Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();
}
});
powerSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
msg="PowerSaver Mode is Switched OFF";
if(isChecked){
msg="PowerSaver Mode is Switched ON";
}
Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();
}
});
}
}
+0
使用此鏈接:http://vimaltuts.com/android-tutorial-for-beginners/android-custom-checkbox –
+0
那麼,在這種情況下,有一個固定數量的cbs。但在我的情況下,這個數字是在運行時計算的。 –
+0
你可以使用循環,然後創建n個數量的cbs。 –
相關問題
- 1. jquery移動動態複選框樣式
- 2. Android動態複選框
- 3. 的Android定製複選框
- 4. 樣式複選框
- 5. Android ActionBar溢出複選框樣式
- 6. jQuery禁用複選框樣式動作
- 7. jquery移動複選框樣式問題
- 8. 自定義複選框樣式
- 9. WPF的自定義複選框樣式
- 10. 動態複選框
- 11. 動態更改複選框樣式爲默認
- 12. CSS複選框樣式
- 13. Datagrid複選框樣式
- 14. Codename one複選框樣式
- 15. 引導複選框樣式
- 16. HTML/CSS樣式複選框
- 17. CSS複選框樣式
- 18. 複選框的CSS樣式
- 19. CSS3樣式複選框
- 20. XPages複選框樣式
- 21. 複選框樣式 - 如何?
- 22. android動態更改複選框問題
- 23. 在C#中控制動態複選框
- 24. 複選框狀態android eclipse
- 25. 樣式複選框和單選按鈕
- 26. android動態更改樣式
- 27. Android複選框 - 控制EditText?
- 28. PHP - 複選框狀態保存。動態#複選框
- 29. 自定義樣式的複選框不加載動態加載內容的樣式
- 30. 動態複選框中angularjs
你試過調用三個參數['CheckBox' ](http://developer.android.com/reference/android/widget/CheckBox.html#CheckBox%28android.content.Context,%20android.util.AttributeSet ,%20int%29)構造函數?查看'CheckBox'的源代碼,看起來調用1參數構造函數只是簡單地調用了3個參數的內部樣式:'this(context,null,com.android.internal.R.attr.checkboxStyle) ;'。我想說,嘗試傳遞你自己的風格作爲第三個參數是有道理的。 –