Android开发中Intent和startActivityForResult传值

先看效果:


图一  第一个界面初始效果                                            图二 第二个界面输入值的效果                               图三 关闭第二个界面将结果显示在第一个界面中

第一个Activity实现了页面的跳转及结果的显示

import com.example.zl.R;

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

public class MainActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jieshou_main);
	}

	public void button1click(View view){
		Intent intent=new Intent(this,BookActiviey.class);
		this.startActivityForResult(intent, 100);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		// 接受数据
		String name=data.getStringExtra("bookname");
		String price=data.getStringExtra("price");
		
		//输出到界面
		TextView textview=(TextView)findViewById(R.id.result);
		textview.setText("你输入的书名是:"+name+"\n价格是:"+price);
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}

第一个界面实现整体布局:

<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击买书"
        android:onClick="button1click" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

第二个Activity的代码:


package com.hnjdzy.JieShouPara;

import com.example.zl.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class BookActiviey extends Activity {


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jieshou_book);
	}
	
	public void submitclick(View view)
	{
		//从界面上读取信息
		EditText edittext1=(EditText)findViewById(R.id.title);
		String TitleName=edittext1.getText().toString();
		
		EditText edittext2=(EditText)findViewById(R.id.price);
		String Price=edittext2.getText().toString();
		
		//传给调用者
		Intent data=new Intent();
		data.putExtra("bookname", TitleName);
		data.putExtra("price", Price);
		
		this.setResult(20, data);
		
		this.finish();
		
	}

}

第二个界面实现的布局:

<?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" >


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




    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="书籍名称:"
        android:textSize="25sp" />
    
        <EditText
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />
    </LinearLayout>
    
        <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">




    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="价格:"
        android:textSize="25sp" />
    
        <EditText
        android:id="@+id/price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />
    </LinearLayout>


        <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交并关闭" 
            android:textSize="25sp"
            android:layout_gravity="center"
            android:onClick="submitclick"/>


</LinearLayout>






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值