android之双向传递数据

向下一个活动传递数据

使用Intent对象中的putExtra()方法,可以把我们想要专递的数据暂存在Intent中,启动另一个活动后,只需要将这些数据从Intent中取出即可。

Bundle可以传递多种数据,是一种类似map的key-value数据结构

通常我们可以使用Bundle存储数据,然后再讲Bundle放入Intent中


向上一个活动传递数据

我们可以使用Activity中的startActivityForResult()方法用于启动活动,但这个方法期望在活动销毁的时候能够返回一个结果给上一个活动。


下面我们就使用案例来实现向下传递数据和向上传递数据(求和案例)

首先我们需要设置布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.extrademo.MainActivity"
    android:stretchColumns="1"
    tools:ignore="MergeRootFrame">
    
    <TableRow>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
			android:text="@string/txt1"
        />
        
        <EditText 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/hint_num1"
            android:id="@+id/num1"
            />
    </TableRow>
    <TableRow>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
			android:text="@string/txt2"
        />
        <EditText 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/hint_num2"
            android:id="@+id/num2"
            />
    </TableRow>
    
    <TableRow>
		<Button 
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    android:text="@string/btn1"
		    android:layout_span="2"
		    android:id="@+id/btn1"
		    />        
    </TableRow>
    
    <TableRow>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
			android:text="@string/sum"
			android:id="@+id/sum"            
            />
    </TableRow>
    
</TableLayout>

然后再创建一个活动SecondActivity,并在Mainifest注册

<!-- 第二个活动 -->
<activity
	android:name="com.example.extrademo.SecondActivity">
</activity>

然后我们开始在主活动中写入逻辑代码,将数据传入到SecondActivity

public class MainActivity extends ActionBarActivity {

    private EditText ed_a;
	private EditText ed_b;
	private TextView tv;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //获取对应的视图
        ed_a = (EditText)findViewById(R.id.num1);
        ed_b = (EditText)findViewById(R.id.num2);
        Button btn = (Button)findViewById(R.id.btn1);
        tv = (TextView)findViewById(R.id.sum);
        
        //添加点击事件
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String a = ed_a.getText().toString();
				String b = ed_b.getText().toString();
				//创建意图
				Intent intent = new Intent(MainActivity.this,SecondActivity.class);
				//设置Bundle对象用于存储数据,然后将Bundle存入intent中
				Bundle bundle = new Bundle();
				bundle.putString("a", a);
				bundle.putString("b", b);
				//将Bundle存入intent中
				intent.putExtras(bundle);
				//开启活动并发送一个200的请求码
				startActivityForResult(intent, 200);
			}
		});
    }
	/**
	 * 回调方法
	 * @param requestCode 请求码
	 * @param resultCode 结果码
	 * @param data 数据
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		//当请求码为200结果码和返回的结果码相同则获取数据,显示数据
		if (requestCode == 200) {
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				int result = bundle.getInt("result");
				tv.setText("结果:" + result);
			}
		}
	}
}

再然后我们在SecondActivity中将MainActivity传递的数据获取,处理,并将结果返回到MainActivity中显示出来

public class SecondActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//获取到启动SecondActivity的Intent
		Intent intent = this.getIntent();
		//获取传输过来的数据
		Bundle bundle = intent.getExtras();
		String a = bundle.getString("a");
		String b = bundle.getString("b");
		//将传入过来的结果进行转型   在这里如果不输入数字将会报错,由于只是了解方法,我们就不再进行优化了
		int result = Integer.parseInt(a) + Integer.parseInt(b);
		//求和的结果放入bundle中
		bundle.putInt("result", result);
		//将bundle放入intent中
		intent.putExtras(bundle);
		//设置返回结果的结果码和意图
		setResult(RESULT_OK, intent);
		//销毁活动
		finish();
	}
}

下面是string.xml文件中的数据

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">求和</string>
    <string name="action_settings">Settings</string>
    <!-- 左边提示文本 -->
    <string name="txt1">数字1</string>
    <string name="txt2">数字2</string>
    <!-- 提示文字 -->
    <string name="hint_num1">请输入第一个数字</string>
	<string name="hint_num2">请输入第二个数字</string>
	<!-- 按钮名字 -->
	<string name="btn1">求和</string>
	
	<!-- 显示数据 -->
	<string name="sum">求和:</string>
</resources>

运行结果如下:




  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值