<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="多选控件:" android:id="@+id/label"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="AA" android:id="@+id/plain_cb"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="BB" android:id="@+id/serif_cb"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="CC" android:id="@+id/bold_cb"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="DD" android:id="@+id/italic_cb"/> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="获取checkbox" android:id="@+id/button1"/> </LinearLayout>
package com.example.yanlei.mytk; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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 AppCompatActivity { private CheckBox plainCB;
private CheckBox serifCB;
private CheckBox boldCB;
private CheckBox italicCB;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); plainCB=(CheckBox)findViewById(R.id.plain_cb);
serifCB=(CheckBox)findViewById(R.id.serif_cb);
boldCB=(CheckBox)findViewById(R.id.bold_cb);
italicCB=(CheckBox)findViewById(R.id.italic_cb);
button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result="";
if(plainCB.isChecked()){
result+=plainCB.getText()+"、";
}
if(serifCB.isChecked()){
result+=serifCB.getText()+"、";
}
if(boldCB.isChecked()){
result+=boldCB.getText()+"、";
}
if(italicCB.isChecked()){
result+=italicCB.getText()+"、";
}
if(!"".equals(result)){
result=result.substring(0,result.length()-1);
}else{
result="你没有选择!!!";
}
setTitle("Checked:"+result);
}
}); }
}