Android SDK 开发范例大全 ---3.11返回数据到前一个Activity
2015-12-03 19:42
471 查看
上一个范例中,我们好不容易将数据从Activity1传递到Activity2,假设要回到Activity1,数据不会要再封装一次吧?而且前一个Activity1早就被程序destroy了,倘若在Activity1最后以finish()结束程序,再通过Activity2将数据采用Bundle的方式通过新打开Activity1的传递参数,这样的做法虽然也可以恢复User输入得数据,但是并不符合我们的预期,尤其是User曾经输入过的数据,如果不小心回到上一页,数据就会消失不见。
如果要在次页面加上一个“回上一页”的按钮,而非通过模拟器的回复键,且回上一页后又能保留之前输入的信息,那么
src/com.helloworld/HelloworldActivity.java
package com.helloworld;
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 android.widget.RadioButton;
import android.widget.TextView;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private Button mButton1,mButton2;
private TextView mTextView1;
private EditText et;
private RadioButton rb1,rb2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
//取得输入得身高
et = (EditText) findViewById(R.id.pHeight);
double height = Double.parseDouble(et.getText().toString());
//取得输入得性别
String sex = "";
rb1 = (RadioButton) findViewById(R.id.myRadioButton01);
rb2 = (RadioButton) findViewById(R.id.myRadioButton02);
if(rb1.isChecked()){
sex = "M";
}else{
sex = "F";
}
//new 一个Intent对象,并指定class
Intent intent = new Intent();
intent.setClass(HelloworldActivity.this, HidiActivity.class);
//new 一个Bundle对象,并将要传递的数据传人
Bundle bundle = new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
//将Bundle对象assgin给Intent
intent.putExtras(bundle);
//调用HelloworldActivity
startActivityForResult(intent,0);
}
});
}
//重写onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(resultCode){
case RESULT_OK:
Bundle bunde = data.getExtras();
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
et.setText(""+height);
if(sex.equals("M")){
rb1.setChecked(true);
}else{
rb2.setChecked(true);
}
break;
default:
break;
}
}
}
src/com.helloworld/HidiActivity.java
package com.helloworld;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HidiActivity extends Activity {
private Button mButton2;
private Intent intent;
private Bundle bunds;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//取得Intent中的Bundle对象
intent = this.getIntent();
bunds = this.getIntent().getExtras();
//取得Bundle对象的数据
String sex = bunds.getString("sex");
double height = bunds.getDouble("height");
//判断性别
String sexText = "";
if(sex.equals("M")){
sexText = "男性";
}else{
sexText = "女性";
}
//取得标准体重
String weight = getWeight(sex,height);
TextView tv1 = (TextView) findViewById(R.id.myTextView02);
tv1.setText("你是一位"+sexText+"\n你的身高是"+height+"厘米\n你的标准体重是"+weight+"公斤");
mButton2 = (Button) findViewById(R.id.myButton2);
mButton2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
HidiActivity.this.setResult(RESULT_OK, intent);
HidiActivity.this.finish();
}
});
}
private String getWeight(String sex, double height) {
// TODO Auto-generated method stub
String weight = "";
if(sex.equals("M")){
weight = format((height-80)*0.7);
}else{
weight = format((height-70)*0.6);
}
return weight;
}
private String format(double d) {
// TODO Auto-generated method stub
NumberFormat formatter = new DecimalFormat("0.0");
String s = formatter.format(d);
return s;
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView1"
android:textSize="34sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/hello" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/myTableRow01"
>
<TextView
android:id="@+id/myTextView2"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="性别:" />
<RadioGroup
android:id="@+id/myRadioGroup01"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/myRadioButton01"
android:text="男"
android:layout_width="wrap_content"
/>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/myRadioButton02"
android:text="女"
android:layout_width="wrap_content"
/>
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/myTableRow02"
>
<TextView
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="身高:"
/>
<EditText
android:id="@+id/pHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/myTableRow02"
>
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" />
</TableRow>
</TableLayout>
res/layout/mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回上一页" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloworldActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="HidiActivity"></activity>
</application>
</manifest>
如果要在次页面加上一个“回上一页”的按钮,而非通过模拟器的回复键,且回上一页后又能保留之前输入的信息,那么
src/com.helloworld/HelloworldActivity.java
package com.helloworld;
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 android.widget.RadioButton;
import android.widget.TextView;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private Button mButton1,mButton2;
private TextView mTextView1;
private EditText et;
private RadioButton rb1,rb2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
//取得输入得身高
et = (EditText) findViewById(R.id.pHeight);
double height = Double.parseDouble(et.getText().toString());
//取得输入得性别
String sex = "";
rb1 = (RadioButton) findViewById(R.id.myRadioButton01);
rb2 = (RadioButton) findViewById(R.id.myRadioButton02);
if(rb1.isChecked()){
sex = "M";
}else{
sex = "F";
}
//new 一个Intent对象,并指定class
Intent intent = new Intent();
intent.setClass(HelloworldActivity.this, HidiActivity.class);
//new 一个Bundle对象,并将要传递的数据传人
Bundle bundle = new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
//将Bundle对象assgin给Intent
intent.putExtras(bundle);
//调用HelloworldActivity
startActivityForResult(intent,0);
}
});
}
//重写onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(resultCode){
case RESULT_OK:
Bundle bunde = data.getExtras();
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
et.setText(""+height);
if(sex.equals("M")){
rb1.setChecked(true);
}else{
rb2.setChecked(true);
}
break;
default:
break;
}
}
}
src/com.helloworld/HidiActivity.java
package com.helloworld;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HidiActivity extends Activity {
private Button mButton2;
private Intent intent;
private Bundle bunds;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//取得Intent中的Bundle对象
intent = this.getIntent();
bunds = this.getIntent().getExtras();
//取得Bundle对象的数据
String sex = bunds.getString("sex");
double height = bunds.getDouble("height");
//判断性别
String sexText = "";
if(sex.equals("M")){
sexText = "男性";
}else{
sexText = "女性";
}
//取得标准体重
String weight = getWeight(sex,height);
TextView tv1 = (TextView) findViewById(R.id.myTextView02);
tv1.setText("你是一位"+sexText+"\n你的身高是"+height+"厘米\n你的标准体重是"+weight+"公斤");
mButton2 = (Button) findViewById(R.id.myButton2);
mButton2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
HidiActivity.this.setResult(RESULT_OK, intent);
HidiActivity.this.finish();
}
});
}
private String getWeight(String sex, double height) {
// TODO Auto-generated method stub
String weight = "";
if(sex.equals("M")){
weight = format((height-80)*0.7);
}else{
weight = format((height-70)*0.6);
}
return weight;
}
private String format(double d) {
// TODO Auto-generated method stub
NumberFormat formatter = new DecimalFormat("0.0");
String s = formatter.format(d);
return s;
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView1"
android:textSize="34sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/hello" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/myTableRow01"
>
<TextView
android:id="@+id/myTextView2"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="性别:" />
<RadioGroup
android:id="@+id/myRadioGroup01"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/myRadioButton01"
android:text="男"
android:layout_width="wrap_content"
/>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/myRadioButton02"
android:text="女"
android:layout_width="wrap_content"
/>
</RadioGroup>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/myTableRow02"
>
<TextView
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="身高:"
/>
<EditText
android:id="@+id/pHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/myTableRow02"
>
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" />
</TableRow>
</TableLayout>
res/layout/mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回上一页" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloworldActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="HidiActivity"></activity>
</application>
</manifest>
相关文章推荐
- 简单柱状图的实现
- AndroidStudio安装SVN遇到的坑爹的问题Can't use Subversion command line client
- android studio快捷键大全
- Android 常用 Style 属性
- android反编译apk常用工具
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- Android studio debug使用正式的签名
- Android PopupWindow with ListView的一些问题
- android init.rc文件语法详解(续)
- android之ContentProvider和Uri详解
- android学习5——画图问题
- 如何优化 Android Studio 启动、编译和运行速度?
- Android Studio关于SVN的相关配置简介
- Android中利用shape定制控件边框
- android More Resource Types
- android-Style Resource
- android SD卡 数据存储代码
- Android自学之路,DrawerLayout must be measured with MeasureSpec.EXACTLY.错误
- 加速你的Android应用
- android 5.1 出现有些apk反向问题解决方法