main.xml
..........................................................................................................................................
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击"/>
</LinearLayout>
edit.xml
...................................................................................................................................
<?xmlversion="1.0"encoding="utf-8"?>
<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="11pt"/>
<RadioGroup
android:id="@+id/sex"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<RadioButton
android:id="@+id/nan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:"
android:textSize="11pt"/>
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/Box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
/>
<CheckBox
android:id="@+id/Box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="黄色"
/>
<CheckBox
android:id="@+id/Box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色"
/>
</LinearLayout>
</TableRow>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="确定"/>
</TableLayout>
......................................................................................
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.CheckBox;
importandroid.widget.CompoundButton;
importandroid.widget.RadioButton;
importandroid.widget.RadioGroup;
importandroid.widget.RadioGroup.OnCheckedChangeListener;
importandroid.widget.Toast;
publicclassMainActivityextendsActivity{
RadioButtonradio,radio1;
CheckBoxBox,Box1,Box2;
AlertDialogalert;
Stringtemp=null;
Stringcolor=null;
intnum=0;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Buttonbn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewarg0){
finalAlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);
Viewview=LayoutInflater.from(MainActivity.this).inflate(R.layout.edit,null);
builder.setView(view);
builder.setTitle("选择:");
builder.setMessage("请做出你最后的抉择:");
alert=builder.create();
alert.show();
RadioGroupgroup=(RadioGroup)view.findViewById(R.id.sex);
radio=(RadioButton)view.findViewById(R.id.nan);
radio1=(RadioButton)view.findViewById(R.id.nv);
Box=(CheckBox)view.findViewById(R.id.Box1);
Box1=(CheckBox)view.findViewById(R.id.Box2);
Box2=(CheckBox)view.findViewById(R.id.Box3);
Buttonbutton=(Button)view.findViewById(R.id.button);
group.setOnCheckedChangeListener(newOnCheckedChangeListenerImp());
Box.setOnCheckedChangeListener(newOnCheckBox());
Box1.setOnCheckedChangeListener(newOnCheckBox1());
Box2.setOnCheckedChangeListener(newOnCheckBox2());
button.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
Stringresult="";
if(Box.isChecked()){
result+=Box.getText().toString();
}
if(Box1.isChecked()){
result+=Box1.getText().toString();
}
if(Box2.isChecked()){
result+=Box2.getText().toString();
}
if(result!=""){
Toast.makeText(MainActivity.this,"你的选择是:性别:"+temp+",你选了"+num+"种颜色分别为:"+result,Toast.LENGTH_LONG).show();
}
alert.dismiss();
}
});
}
});
}
privateclassOnCheckedChangeListenerImpimplementsOnCheckedChangeListener{
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
if(MainActivity.this.radio.getId()==checkedId){
temp="男";
}
if(MainActivity.this.radio1.getId()==checkedId){
temp="女";
}
}
}
privateclassOnCheckBoximplementsandroid.widget.CompoundButton.OnCheckedChangeListener{
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
if(Box.isChecked()){
System.out.println(Box.getText().toString());
num++;
}else{
num--;
}
}
}
privateclassOnCheckBox1implementsandroid.widget.CompoundButton.OnCheckedChangeListener{
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
if(Box.isChecked()){
System.out.println(Box1.getText().toString());
num++;
}else{
num--;
}
}
}
privateclassOnCheckBox2implementsandroid.widget.CompoundButton.OnCheckedChangeListener{
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
if(Box.isChecked()){
System.out.println(color=Box2.getText().toString());
num++;
}else{
num--;
}
}
}
}
原文链接:https://www.f2er.com/xml/299911.html