`
yidongkaifa
  • 浏览: 4069270 次
文章分类
社区版块
存档分类
最新评论

Android入门篇三:使用静态变量在Activity之间传递数据

 
阅读更多

上次给大家介绍了如何使用意图在Activity之间传递数据,这次讲解一下如何使用静态变量来传递数据,

原理其实很简单,就是在接收端的Avtivity里面设置static的变量,在发送端这边改变静态变量的值,然后启动意图。

效果图为:

发送端截图:

接收端截图:



那么就直接给代码了:

一、MainActivity.java

package com.intent.activity;

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

public class MainActivity extends Activity {
	private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.btOpenOtherActivity);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//定义一个意图
				Intent intent = new Intent(MainActivity.this,OtherActivity.class);
				//改变OtherActivity的三个静态变量的值
				OtherActivity.name = "wulianghuan";
				OtherActivity.age = "22";
				OtherActivity.address = "上海闵行";
				startActivity(intent);
			}
		});
    }
}

二、OtherActivity.java

package com.intent.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class OtherActivity extends Activity {
	public static String name;
	public static String age;
	public static String address;
	private TextView text_name;
	private TextView text_age;
	private TextView text_address;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		text_name = (TextView) findViewById(R.id.name);
		text_age = (TextView) findViewById(R.id.age);
		text_address = (TextView) findViewById(R.id.address);	
		//设置文本框的数据
		text_name.setText("姓名:"+name);
		text_age.setText("年龄:"+age);
		text_address.setText("地址:"+address);
	}
}

布局文件

三、main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是:MainActivity" />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btOpenOtherActivity"
        android:text="使用静态变量传递数据"/>

</LinearLayout>

四、other.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是:MainActivity" />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btOpenOtherActivity"
        android:text="使用意图传递数据"/>

</LinearLayout>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics