(1) 界面布局:
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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”
tools:context=".MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="18dp" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:id="@+id/radio_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radio_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="职业:"
android:textSize="18dp" />
<EditText
android:id="@+id/et_professor"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<RadioGroup android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Married">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="婚否:"
android:textSize="18dp" />
<RadioButton
android:id="@+id/radio_YesMarrige"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是" />
<RadioButton
android:id="@+id/radio_NoMarrige"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="否" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="18dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/ck_basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/ck_football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
<CheckBox
android:id="@+id/ck_volleyball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排球" />
<CheckBox
android:id="@+id/ck_lunhua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="轮滑" />
<CheckBox
android:id="@+id/ck_yumaoqiu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="羽毛球" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/ck_diaoyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="钓鱼" />
<CheckBox
android:id="@+id/ck_wudao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="舞蹈" />
<CheckBox
android:id="@+id/ck_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐" />
<CheckBox
android:id="@+id/ck_paobu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑步" />
<CheckBox
android:id="@+id/ck_swimming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_current"
android:text="确定"/>
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置" />
</LinearLayout>
</GridLayout>
</android.support.constraint.ConstraintLayout>
Show_Result.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".Show_Result">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="21dp" />
<TextView
android:id="@+id/tv_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="21dp" />
<TextView
android:id="@+id/tv_pro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="21dp" />
<TextView
android:id="@+id/tv_getmarried"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="21dp" />
<TextView
android:id="@+id/tv_enjoy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="21dp" />
<ListView
android:id="@+id/show_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
(2) 逻辑代码的实现:
MainActivity.java;
package xatu.cn.androidtest_two;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText username, professor;
private RadioGroup radioGroup_sex, radioGroup_married;
private Button button_current, button_reset;
private RadioButton radioButton_getMarried, radioButton_NoMarried, man, woman;
private CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6,
checkBox7, checkBox8, checkBox9, checkBox10;
private List checkBoxList = new ArrayList();
private String selectedType_sex = “”, SelectedType1_marrige = “”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.et_username);
professor = findViewById(R.id.et_professor);
button_reset = findViewById(R.id.btn_reset);
checkBox1 = findViewById(R.id.ck_basketball);
checkBox2 = findViewById(R.id.ck_football);
checkBox3 = findViewById(R.id.ck_diaoyu);
checkBox4 = findViewById(R.id.ck_lunhua);
checkBox5 = findViewById(R.id.ck_music);
checkBox6 = findViewById(R.id.ck_paobu);
checkBox7 = findViewById(R.id.ck_swimming);
checkBox8 = findViewById(R.id.ck_volleyball);
checkBox9 = findViewById(R.id.ck_wudao);
checkBox10 = findViewById(R.id.ck_yumaoqiu);
checkBoxList.add(checkBox1);
checkBoxList.add(checkBox2);
checkBoxList.add(checkBox3);
checkBoxList.add(checkBox4);
checkBoxList.add(checkBox5);
checkBoxList.add(checkBox6);
checkBoxList.add(checkBox7);
checkBoxList.add(checkBox8);
checkBoxList.add(checkBox9);
checkBoxList.add(checkBox10);
//男女性别的选择
radioGroup_sex = findViewById(R.id.radio_sex);
man = findViewById(R.id.radio_boy);
woman = findViewById(R.id.radio_girl);
radioGroup_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int id) {
if (id == R.id.radio_boy) {
selectedType_sex = man.getText().toString();
} else {
selectedType_sex = woman.getText().toString();
}
}
});
//是否已婚的选择
radioGroup_married = findViewById(R.id.Married);
radioButton_getMarried = findViewById(R.id.radio_YesMarrige);
radioButton_NoMarried = findViewById(R.id.radio_NoMarrige);
radioGroup_married.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int id) {
if (id == R.id.radio_YesMarrige) {
SelectedType1_marrige = radioButton_getMarried.getText().toString();
} else {
SelectedType1_marrige = radioButton_NoMarried.getText().toString();
}
}
});
//单击确定的按钮
button_current = findViewById(R.id.btn_current);
button_current.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Show_Result.class);
//对复选框的选择操作
StringBuffer sb = new StringBuffer();
//遍历集合中的checkBox,判断是否选择,获取选中的文本
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()) {
sb.append(checkbox.getText().toString()+" ");
}
}
if (sb != null && "".equals(sb.toString())) {
Toast.makeText(getApplicationContext(), "请至少选择一个", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show();
intent.putExtra("enjoy", sb.toString().trim());
}
intent.putExtra("et_username", username.getText().toString().trim());
intent.putExtra("et_professor", professor.getText().toString().trim());
intent.putExtra("selectedType_sex", selectedType_sex.toString().trim());
intent.putExtra("selectedType_marrige", SelectedType1_marrige.toString().trim());
// intent.putExtra(“woman”,woman.getText().toString().trim());
Bundle bundle = new Bundle();
bundle.putString(“selected_sex”, selectedType_sex);
bundle.putString(“selected_marrige”, SelectedType1_marrige);
intent.putExtras(bundle);
startActivity(intent);
}
});
//单击重置的按钮
button_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username.getText().clear();
professor.getText().clear();
radioGroup_sex.clearCheck();
radioGroup_married.clearCheck();
//对复选框的选择操作
StringBuffer sb = new StringBuffer();
//遍历集合中的checkBox,判断是否选择,如果已经选择,就需要重置
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()) {
checkbox.setChecked(false);
}
}
}
});
}
}
Show_Result.java
package xatu.cn.androidtest_two;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class Show_Result extends AppCompatActivity {
private TextView tv_name, tv_pro, tv_married, tv_sex, tv_enjoy;
private ListView listView;
private List list = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show__result);
listView=findViewById(R.id.show_listview);
final Intent intent = getIntent();
// 拿到Maincity的用户名并且显示
String name = intent.getStringExtra("et_username");
list.add(name);
tv_name = findViewById(R.id.tv_name);
tv_name.setText("用户名:" + name);
//拿到职业并且显示在TextView
String professor = intent.getStringExtra("et_professor");
list.add(professor);
tv_pro = findViewById(R.id.tv_pro);
tv_pro.setText("职业:" + professor);
//性别选择
String sex = intent.getStringExtra("selected_sex");
list.add(sex);
tv_sex = findViewById(R.id.tv_sex);
tv_sex.setText("性别:" + sex);
// 婚否选择
String marrige = intent.getStringExtra(“selected_marrige”);
list.add(marrige);
tv_married = findViewById(R.id.tv_getmarried);
tv_married.setText(“婚否:” + marrige);
// 爱好的选择
String enjoy = intent.getStringExtra(“enjoy”);
list.add(enjoy);
tv_enjoy = findViewById(R.id.tv_enjoy);
tv_enjoy.setText(“爱好:” + enjoy);
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
}