Activity间中使用Intent传值

主页面用来输入一个值传入第二个页面显示,关闭第二个页面返回一个值

主页布局:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭第二个页面后返回值" />

    <EditText
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/view"
        android:text="" />
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt"
        android:text="转第二页" />
</RelativeLayout>

 

MainActivity,主要通过startActivityForResult来传递请求码,返回数据的时候,第二个页面调用Activity.setResult()方法设置返回Intent以及返回码,需要重写源Activity的onActivityResult()方法以便于接受返回的Intent,在onActivityResult()中会判断请求码和响应码

package com.example.android01;

import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private final static String TAG="MainActivity";
    private final static int REQUEST_CODE = 1;
    private final static String CONTENT = "content";
    private EditText txtEditText = null;
    private Button btnButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtEditText = (EditText) this.findViewById(R.id.txt);
        if (savedInstanceState != null && savedInstanceState.containsKey(CONTENT)) {
            txtEditText.setText(savedInstanceState.getString(CONTENT) + "[2]");
        }
        btnButton = (Button) this.findViewById(R.id.btn);
        btnButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                String str=txtEditText.getText().toString();
                intent.putExtra("fromMain", str);
                // startActivity(intent);
                // 启动需要监听返回值的Activity,并设置请求码:requestCode
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onActivityResult(int, int,
     * android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 当otherActivity中返回数据的时候,会响应此方法
        // requestCode和resultCode必须与请求startActivityForResult()和返回setResult()的时候传入的值一致。
        if (requestCode == REQUEST_CODE && resultCode == SecondActivity.RESULT_CODE) {
            Bundle bundle=data.getExtras();
            String strResult = bundle.getString("result");
            Log.i(TAG,"onActivityResult: "+ strResult);
            Toast.makeText(MainActivity.this, strResult, Toast.LENGTH_LONG).show();
        }
    }

    /*
     * (non-Javadoc) 保存Activity页面状态在onStop之前执行
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        String contentString = txtEditText.getText().toString();
        outState.putString(CONTENT, contentString);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

 

每二个Activity

/**
 * 
 */
package com.example.android01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author Administrator 2014-3-12 下午10:21:17
 */
public class SecondActivity extends Activity {

    private final static String TAG="MainActivity";
    public final static int RESULT_CODE=1;
    private Button btnBack = null;
    private EditText txtContent = null;
    private TextView viewContent = null;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);

        txtContent = (EditText) this.findViewById(R.id.txtContent);
        viewContent = (TextView) this.findViewById(R.id.viewContent);
        btnBack = (Button) this.findViewById(R.id.btnBack);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String strContentString = bundle.getString("fromMain");
        Log.i(TAG,"SecondActivity: "+ strContentString);
        viewContent.setText(strContentString);
        btnBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.putExtra("result", txtContent.getText().toString());
                setResult(RESULT_CODE, intent);// 设置resultCode,onActivityResult()中能获取到
                finish();
            }
        });
    }

}

注意请求码与返回码的对应判断,因为可能有多个请求码与返回码。

posted @ 2014-03-16 17:25  欢醉  阅读(23525)  评论(0编辑  收藏  举报