1.Activity传递参数
MainActivity.java
package zhku.edu.exp4_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Calendar;
/**
* Created by zhang on 2016/10/14.
*/
public class MainActivity extends Activity {
EditText username,password,year;
Button btn_login,btn_register;
double x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_login=(Button)findViewById(R.id.btn_login);
btn_register=(Button)findViewById(R.id.btn_register);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
year=(EditText)findViewById(R.id.year);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et1=username.getText().toString();
String et2=password.getText().toString();
String et3=year.getText().toString();
try{
x=Double.parseDouble(et3);
}catch (Exception e){
e.printStackTrace();
}
//获取当前时间
Calendar currentTime= Calendar.getInstance();
double currentYear=currentTime.get(Calendar.YEAR);
double year=currentYear-x;
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
intent.putExtra("username",et1);
intent.putExtra("password",et2);
intent.putExtra("year",year);
startActivity(intent);
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it=new Intent();
it.setAction("MyActivity");
it.addCategory("android.intent.category.DEFAULT");//可以省略
startActivity(it);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="zhku.edu.exp4_2.MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:singleLine="true"
android:hint="@string/username"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:singleLine="true"
android:hint="@string/password"/>
<EditText
android:id="@+id/year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:singleLine="true"
android:hint="@string/year"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
/>
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginLeft="120dp"
/>
</LinearLayout>
</LinearLayout>
ResultActivity.java
package zhku.edu.exp4_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by zhang on 2016/10/14.
*/
public class ResultActivity extends Activity{
TextView tv_1,tv_2,tv_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv_1=(TextView)findViewById(R.id.tv_1);
tv_2=(TextView)findViewById(R.id.tv_2);
tv_3=(TextView)findViewById(R.id.tv_3);
Intent intent=getIntent();
String username=intent.getStringExtra("username");
String password=intent.getStringExtra("password");
double age =intent.getDoubleExtra("year",0);//如果没输入,则默认值为0
tv_1.setText(username);
tv_2.setText(password);
tv_3.setText(String.valueOf(age)+"岁");
}
}
activity_result.xml
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的用户名:"
android:textSize="25sp"/>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的密码:"
android:textSize="25sp"/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的年龄:"
android:textSize="25sp"/>
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"/>
</LinearLayout>
</LinearLayout>
2.Activity回传参数
MainActivity.java
package zhku.edu.exp4_3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends Activity {
EditText et_hobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_hobby=(EditText) findViewById(R.id.hobby);
et_hobby.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,HobbyActivity.class);
startActivityForResult(intent,0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取回传数据
if(requestCode==0){//对应startActivityForResult的请求码
if(resultCode==1){//对应HobbyActivity总的setResult
Bundle bundle=data.getExtras();
ArrayList<String> hobbies=bundle.getStringArrayList("Hobby");
//从hobbies列举字符串到textview上
for(int i=0;i<hobbies.size();i++){
et_hobby.setText(hobbies.get(i)+",");
}
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="zhku.edu.exp4_3.MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
android:textSize="30sp"
/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone"
android:textSize="30sp"
/>
<EditText
android:id="@+id/hobby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hobby"
android:textSize="30sp"
/>
</LinearLayout>
activity_hobby.xml
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/yourhobby"
android:textSize="35sp"/>
<CheckBox
android:id="@+id/program"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编程"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑步"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/fish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="钓鱼"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="踢足球"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打篮球"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/climb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爬山"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<CheckBox
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="听音乐"
android:textSize="20sp"
android:layout_marginLeft="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_marginLeft="10dp"
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认选择"/>
<Button
android:layout_marginLeft="120dp"
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消返回"/>
</LinearLayout>
</LinearLayout>
HobbyActivity.java
package zhku.edu.exp4_3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhang on 2016/10/15.
*/
public class HobbyActivity extends Activity implements View.OnClickListener {
List<Integer> ids=new ArrayList<Integer>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hobby);
ids.add(R.id.program);
ids.add(R.id.swim);
ids.add(R.id.run);
ids.add(R.id.fish);
ids.add(R.id.football);
ids.add(R.id.basketball);
ids.add(R.id.climb);
ids.add(R.id.music);
findViewById(R.id.btn_ok).setOnClickListener(this);
findViewById(R.id.btn_cancel).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_cancel){
setResult(0,null);//null的Intent返回
finish();
}
ArrayList<String> hobbies=new ArrayList<String>();
for(Integer id : ids){
CheckBox chk=(CheckBox)findViewById(id);
if(chk.isChecked()) {
hobbies.add(chk.getText().toString());
}
}
Intent intent=new Intent();
intent.setClass(this,MainActivity.class);
Bundle bundle=new Bundle();
bundle.putStringArrayList("Hobby",hobbies);
intent.putExtras(bundle);
setResult(1,intent);
finish();
}
}