首页 > 代码库 > Android 界面跳转及数据交换

Android 界面跳转及数据交换

作者:卿笃军

原文地址:http://blog.csdn.net/qingdujun/article/details/40015757


本文演示:Android 界面跳转及数据交换,通过一个小Demo展示全部过程。

效果如下所示:

1)MainActivity.java

package doogle.xian.bundletest;

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


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText name = (EditText)findViewById(R.id.name);
				EditText age = (EditText)findViewById(R.id.age);
				CData send = new CData(name.getText().toString(),Integer.parseInt(age.getText().toString())); 
				//创建一个Bundle对象
				Bundle data = http://www.mamicode.com/new Bundle();>
2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="doogle.xian.bundletest.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="传给Activity2:" />
    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:hint="name" />
    <EditText
        android:id="@+id/age"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:hint="age" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/age"
        android:text="开启activity2"/>

</RelativeLayout>

3)ResultActivity.java

package doogle.xian.bundletest;

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

public class ResultActivity extends Activity {
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        TextView tv1 = (TextView)findViewById(R.id.tv1);
        TextView tv2 = (TextView)findViewById(R.id.tv2);
        //获得启动该Result的Intent
        Intent intent = getIntent();
        //直接通过Intent取出它所携带的Bundle数据包中的数据
        CData accept = (CData)intent.getSerializableExtra("mydata");
        tv1.setText("姓名"+accept.getName());
        tv2.setText("年龄"+accept.getAge());
    }
}
4)result.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/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="接收到的数据:" />
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>
5)CData.java

package doogle.xian.bundletest;

import java.io.Serializable;

public class CData implements Serializable{
	private static final long serialVersionUID = 1L;
	String name;
	int age;
	public CData(String n, int a) {
		name = n;
		age = a;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
6)AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="doogle.xian.bundletest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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 -->
        <activity
            android:name=".ResultActivity"
            android:label="Activity2" >
        </activity>
    </application>

</manifest>

原文地址:http://blog.csdn.net/qingdujun/article/details/40015757


Android 界面跳转及数据交换