实现两个Activity参数回传。在第一个界面上可直接输入用户名和手机号。当点击兴趣框输入时,会跳转到HobbyActibity界面。在兴趣界面实现多选。选择完毕后点击确定跳转到原界面,兴趣框一栏会显示所选择的兴趣。若在兴趣界面点击取消,则直接回到原界面,无选择。
MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_margin="10dp"
tools:context="com.zhku.exp4_2.MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入用户名"
android:textSize="20sp" />
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入手机号"
android:textSize="20sp"
android:layout_marginTop="5dp" />
<EditText
android:id="@+id/hobby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="选择你的爱好"
android:textSize="20sp"
android:layout_marginTop="5dp" />
</LinearLayout>
package com.zhku.exp4_2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText hobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hobby = (EditText)findViewById(R.id.hobby);
hobby.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent it = new Intent();
it.setClass(MainActivity.this,HobbyActivity.class);
startActivityForResult(it,0);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == 0){
if(resultCode == 1){
Bundle bundle = data.getExtras();
ArrayList<String> hobbies = bundle.getStringArrayList("Hobby");
//从hobbies列举字符串,显示在界面上
String out = hobbies.toString();
bundle.putString("Hobby",out);
hobby.setText(out+"");
}
}
}
}
HobbyActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/hob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你的爱好是"
android:textSize="25sp"
android:layout_margin="10sp"
android:textColor="#000000" />
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<CheckBox
android:id="@+id/program"
android:text="编程"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/swim"
android:text="游泳"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/run"
android:text="跑步"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/fish"
android:text="钓鱼"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/soccer"
android:text="踢足球"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/basketball"
android:text="打篮球"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/mountain"
android:text="爬山"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<CheckBox
android:id="@+id/music"
android:text="听音乐"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<Button
android:id="@+id/choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认选择"
android:textSize="15sp" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消返回"
android:textSize="15sp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
package com.zhku.exp4_2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import java.util.ArrayList;
import java.util.List;
public class HobbyActivity extends AppCompatActivity {
private Button choose;
private Button cancel;
List<Integer> ids = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hobby);
choose = (Button) findViewById(R.id.choose);
cancel = (Button) findViewById(R.id.cancel);
ids.add(R.id.program);
ids.add(R.id.swim);
ids.add(R.id.run);
ids.add(R.id.fish);
ids.add(R.id.soccer);
ids.add(R.id.basketball);
ids.add(R.id.mountain);
ids.add(R.id.music);
choose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v.getId() == R.id.cancel) {
setResult(0, null); //null的intent返回,说明用户未选择
finish();
}
ArrayList<String> hobbies = new ArrayList<String>();
for (Integer id : ids) { //通过数组遍历每个 CheckBox 的选中情况
CheckBox chk = (CheckBox) findViewById(id);
if (chk.isChecked()) {
hobbies.add(chk.getText().toString());
}
}
Bundle bundle = new Bundle();
bundle.putStringArrayList("Hobby", hobbies);
Intent it = new Intent();
it.putExtras(bundle);
setResult(1, it); //设置返回
finish();
}
});
cancel.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
finish();
}
});
}
}
实现效果: