Android学习笔记(六)——从Activity返回数据

1、在实际应用中,我们不仅要向Activity中传数据,也要从Activity中返回数据。虽然传递数据和返回数据类似,也可以采用前面四篇中提到的4种方法,但是一般建议采用Intent对象的方式来返回数据,使用这种方式返回数据,需要使用startActivityForResult方法来显示Activity;

2、新建Android项目“android_intent_forresult”,打开布局文件“activity_main.xml”,添加“LinearLayout”、“TextView”、“EditView”等标签,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/one"
            android:layout_width="20dp"
            android:layout_height="wrap_content" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" + " >
        </TextView>

        <EditText
            android:id="@+id/two"
            android:layout_width="20dp"
            android:layout_height="wrap_content" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" = " >
        </TextView>

        <EditText
            android:id="@+id/result"
            android:layout_width="20dp"
            android:layout_height="wrap_content" >
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="计算结果" >
    </Button>

</LinearLayout>
效果:


3、新建布局文件“other.xml”,添加“TextView”、“EditView”、“Button”标签,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TextView>

        <EditText
            android:id="@+id/three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回结果" >
    </Button>

</LinearLayout>
4、新建“OtherActivity.java”文件,并使其继承“Activity”,添加“onCreate”方法,代码如下:

package com.android.myintent;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
	}
}
5、在“AndroidManifest.xml”清单文件中加入“Activity”,加入代码:

        <activity android:name=".OtherActivity" >
        </activity>

6、在“Main.java”中添加Button成员和“setOnClickListener”,实现两个Button的跳转,点击第一个Activity后,出现第二个Activity;在此方法内部创建意图,用“startActivityForResult”启动意图,并在Main类里重写“onActivityResult”;添加“EditText”成员,实现数据的输入并传入Intent中。代码如下:

package com.android.myintent;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main extends Activity {
	private Button button;
	private final static int REQUESTCODE = 1;// 表示返回的结果码
	private EditText one, two, result; // 数据输入

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		one = (EditText) this.findViewById(R.id.one);
		two = (EditText) this.findViewById(R.id.two);
		result = (EditText) this.findViewById(R.id.result);
		button = (Button) this.findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 点击后获得用户录入的值
				int a = Integer.parseInt(one.getText().toString());
				int b = Integer.parseInt(two.getText().toString());
				// 创建意图
				Intent intent = new Intent(Main.this, OtherActivity.class);
				// 将值传入意图
				intent.putExtra("a", a);
				intent.putExtra("b", b);
				startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果
			}
		});
	}

	// 再重写一个onActivityResult方法,作用是将当前Activity中的数据传递到另一个Activity的意图中后,实现跳转,再回传回来。
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
7、在“OtherActivity.java”文件中添加Button和TextView成员,获取意图中的数据,代码如下:

package com.android.myintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class OtherActivity extends Activity {
	private Button button;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		// 实例化button和textview
		button = (Button) this.findViewById(R.id.button2);
		textView = (TextView) this.findViewById(R.id.msg);
		Intent intent = getIntent(); // 获取Intent
		// 取出Intent中的值
		int a = intent.getIntExtra("a", 0);
		int b = intent.getIntExtra("b", 0);
		textView.setText(a + " + " + b + " = " + " ? ");

	}
}
运行一下,看下效果:


点击“计算结果”,跳转到第二个Activity:


8、回到“Main.java”文件中,从OtherActivity中获取数据并显示,代码如下:

package com.android.myintent;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main extends Activity {
	private Button button;
	private final static int REQUESTCODE = 1;// 表示返回的结果码
	private EditText one, two, result; // 数据输入

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		one = (EditText) this.findViewById(R.id.one);
		two = (EditText) this.findViewById(R.id.two);
		result = (EditText) this.findViewById(R.id.result);
		button = (Button) this.findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 点击后获得用户录入的值
				int a = Integer.parseInt(one.getText().toString());
				int b = Integer.parseInt(two.getText().toString());
				// 创建意图
				Intent intent = new Intent(Main.this, OtherActivity.class);
				// 将值传入意图
				intent.putExtra("a", a);
				intent.putExtra("b", b);
				startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果
			}
		});
	}

	// 再重写一个onActivityResult方法,作用是将当前Activity中的数据传递到另一个Activity的意图中后,实现跳转,再回传回来。
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 2) {// 如果第二个Activity(OtherActivity)正常结束(“2”为返回码resultCode)。
			if (requestCode == REQUESTCODE) {// 如果返回状态为1,即成功返回,就在意图的返回值中取出数据。
				int three = data.getIntExtra("three", 0);// 从第二个Activity中返回意图中的数据。
				result.setText(String.valueOf(three));
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
9、在“OtherActivity.java”文件中,添加点击Button事件,使数据回传~,代码如下:

package com.android.myintent;

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.TextView;

public class OtherActivity extends Activity {
	private Button button;
	private TextView textView;
	private EditText editText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		// 实例化button和textview
		button = (Button) this.findViewById(R.id.button2);
		textView = (TextView) this.findViewById(R.id.msg);
		editText = (EditText) this.findViewById(R.id.three);
		Intent intent = getIntent(); // 获取Intent
		// 取出Intent中的值
		int a = intent.getIntExtra("a", 0);
		int b = intent.getIntExtra("b", 0);
		textView.setText(a + " + " + b + " = " + " ? ");
		// 添加点击事件并回传数据
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();// 重新声明一个意图。
				int three = Integer.parseInt(editText.getText().toString());// 获取输入的值。
				intent.putExtra("three", three); // 将three回传到意图中。
				// 通过Intent对象返回结果,调用setResult方法。
				setResult(2, intent);// resultCode为大于1的数,随意选取,为2即可。
				finish();// 结束当前Activity的生命周期。

			}
		});
	}
}
10、运行,结果:

(1)输入2和3:

(2)单击“计算结果”,跳转:

(3)输入5,单击“返回结果”,数据回传:

实现要点:

(1)在“Main.java”中,创建Intent并启动Activity,调用“startActivityForResult”,并定义当前请求码;

(2)重写“onActivityResult”方法,并设置条件,若满足返回码值,则将第二个Activity中的数据传回来,赋给当前Activity的“result”编辑框;

(3)在“OtherActivity.java”中,再创建一个意图,将数据填写到意图中,通过意图将结果回传(通过“setResult”方法);

(4)结束当前Activity生命周期;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值