Android Checkbox Example

时间:2022-07-16 13:48:12

1. Custom String

打开 “res/values/strings.xml” 文件,

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">CheckBoxDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="chk_ios">IPhone</string>
<string name="chk_android">Android</string>
<string name="chk_windows">Windows Mobile</string>
<string name="btn_display">Display</string>
</resources>

2. CheckBox

打开 “res/layout/activity_main.xml” 文件, 

File : res/layout/activity_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" > <CheckBox
android:id="@+id/chkIos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_ios" /> <CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_android"
android:checked="true" /> <CheckBox
android:id="@+id/chkWindows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_windows" /> <Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" /> </LinearLayout>

注: android:checked="true"   表示默认选中。本例子中,chkAndroid默认选中

3. Code Code

package com.jiangge.checkboxdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast; public class MainActivity extends Activity { private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); addListenerOnChkIos();
addListenerOnButton();
} private void addListenerOnChkIos() {
chkIos = (CheckBox) findViewById(R.id.chkIos); chkIos.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// chkIos 是否被选中
if (((CheckBox)v).isChecked()) {
Toast.makeText(getApplicationContext(), "Bro try android,哈哈",Toast.LENGTH_LONG).show();
}
}
});
} private void addListenerOnButton() {
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
chkWindows = (CheckBox) findViewById(R.id.chkWindows);
btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("IPhone check : ").append(chkIos.isChecked());
result.append("\nAndroid check : ").append(chkAndroid.isChecked());
result.append("\nWindows Mobile check :").append(chkWindows.isChecked()); Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
}); } }

注:

if (((CheckBox)v).isChecked())  向下转型。

StringBuffer result = new StringBuffer();

result .append().append()

4、运行结果:

一启动时的运行结果:

Android Checkbox Example

选中 IPhone (和Android)的运行结果:

Android Checkbox Example

选中IPone 和  Winows Mobile,然后点击 Button 按钮的运行结果:

Android Checkbox Example