Android四大组件的综合应用

/*
(1) 设置两个界面,第一个界面输入姓名、性别、职业、婚否、爱好等,将这些输入信息转到第二个界面并用ListView显示出来
*/
(1) 界面布局:
MainActivity.xml:

xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">



    

        

        
    

    

    
    

    

        

        
    
   
    

    

    
    
    

    

        

        

        

        

        
    

    

        

        

        

        

        
    
    
    


Show_Result.xml:

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">



    

    

    

    

    

    

    

(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;

/*
设置两个界面,第一个界面输入姓名、性别、职业、婚否、爱好等,将这些输入信息转到第二个界面并用ListView显示出来
*/
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);
}
}

你可能感兴趣的:(Android)