Android的小程序---使用Intent在Activity之间传递数据

  • 首先我们先理解一下Activity的生命周期:
    这里写图片描述

  • Android活动程序Activity和意图方法Intent之间的关系
    Activity之间的通信可以使用Intent,在启动另外的Activity的时候,我们可以传递Intent意图,通过Activity类提供的 startActivity方法,Intent类提供了一个从“发件人”到“收件人”的构造方法Intent(Context packageContext, Class

package com.example.intent;
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;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Activity1 extends Activity {
               RadioGroup RG_OS;
             RadioButton RG_OS_RB1;
        RadioButton RG_OS_RB2;
        RadioButton RG_OS_RB3;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        Button button_submit,button_back;
        super.onCreate(savedInstanceState);
        //根据布局文件activity1.xml生成界面
        setContentView(R.layout.activity1);
        //根据XML定义生成取得RadioGroup、RadioButton、Button对象
        RG_OS=(RadioGroup)findViewById(R.id.RG_OS);
        RG_OS_RB1=(RadioButton)findViewById(R.id.RG_OS_RB1);
        RG_OS_RB2=(RadioButton)findViewById(R.id.RG_OS_RB2);
        RG_OS_RB3=(RadioButton)findViewById(R.id.RG_OS_RB3);
        button_submit=(Button)findViewById(R.id.button_submit);
        //使用setOnClickListener注册按钮单击事件监听器
        button_submit.setOnClickListener(new ButtonClickListener());
    }
        //定义按钮button_submit单击监听器。当单击button_submit按钮时,onClick方法被调用
    class ButtonClickListener implements OnClickListener{
        public void onClick(View arg0){
            //新建一个Inter对象
            Intent myintent=new Intent();
            //指定Intent对象的目标组件是Activity2
            myintent.setClass(Activity1.this,Activity2.class);

            //根据用户选择不同的单选按钮,向Intent对象的Extra属性中存不同的值
            if(RG_OS_RB1.isChecked())
            myintent.putExtra("selected", (String)RG_OS_RB1.getText());
            else if (RG_OS_RB2.isChecked())
            myintent.putExtra("selected", (String)RG_OS_RB2.getText());
            else if (RG_OS_RB3.isChecked())
                myintent.putExtra("selected", (String)RG_OS_RB3.getText());
            else
                myintent.putExtra("selected","null");

            //利用startActivity()启动新的Activity,即Activity2
            Activity1.this.startActivity(myintent);
            //关闭当前的Activity
            Activity1.this.finish();
        }
    }
}

(2)新建一个类Activity2,编写Activity2.java的代码如下:

package com.example.intent;
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.TextView;
public class Activity2 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //根据布局文件activity2.xml生成界面
        setContentView(R.layout.activity2);
        Button button_back = (Button)findViewById(R.id.button_back);
        button_back.setOnClickListener(new ButtonClickListener());
        //生成文本框对象
        TextView textview=(TextView)findViewById(R.id.textview);
        //获取Activity传递的Intent
        Intent myintent=this.getIntent();
        String selected_radiobutton=myintent.getStringExtra("selected");
        if(selected_radiobutton=="null")
            textview.setText("没有选择任何系统");
        else 
            textview.setText(selected_radiobutton+"被选中");
    }
    class ButtonClickListener implements OnClickListener{
        public void onClick(View arg0){
            //新建一个Intent对象,并指定启动程序Activity1
            Intent myintent=new Intent();
            myintent.setClass(Activity2.this,Activity1.class);
            Activity2.this.startActivity(myintent);
            Activity2.this.finish();
        }
    }
}

(3)编写Activity2.xml代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第二个Activity" />

    <Button android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />
    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
</LinearLayout>

(4)代码部分编写完成,运行结果如图:
这里写图片描述

这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章