package com.example.checkbox_07;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView textView;
private CheckBox checkBox_01;
private CheckBox checkBox_02;
private CheckBox checkBox_03;
private CheckBox checkBox_04;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
checkBox_01 = (CheckBox) findViewById(R.id.checkBox_01);
checkBox_02 = (CheckBox) findViewById(R.id.checkBox_02);
checkBox_03 = (CheckBox) findViewById(R.id.checkBox_03);
checkBox_04 = (CheckBox) findViewById(R.id.checkBox_04);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
checkBox_01.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
if(checkBox_01.isChecked()){
displayToast("您選擇了"+checkBox_01.getText());
}
}
});
checkBox_02.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
if(checkBox_02.isChecked()){
displayToast("您選擇了"+checkBox_02.getText());
}
}
});
checkBox_03.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
if(checkBox_03.isChecked()){
displayToast("您選擇了"+checkBox_03.getText());
}
}
});
checkBox_04.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView ,boolean isChecked){
if(checkBox_04.isChecked()){
displayToast("您選擇了"+checkBox_04.getText());
}
}
});
btnSubmit.setOnClickListener(new OnClickListener(){
public void onClick(View view){
int num=0;
if(checkBox_01.isChecked()){
num++;
}
if(checkBox_02.isChecked()){
num++;
}
if(checkBox_03.isChecked()){
num++;
}
if(checkBox_04.isChecked()){
num++;
}
displayToast("您選擇了"+num+"項");
}
});
}
public void displayToast(String string){
Toast toast = Toast.makeText(this, string, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 220);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text" />
<CheckBox
android:id="@+id/checkBox_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/checkBox_01"
/>
<CheckBox
android:id="@+id/checkBox_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/checkBox_02"
/>
<CheckBox
android:id="@+id/checkBox_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/checkBox_03"
/>
<CheckBox
android:id="@+id/checkBox_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/checkBox_04"
/>
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit"/>
</LinearLayout>