Android Intent用法一 实现activity之间跳转 并 传参

简述:

刚开始写Android程序, 需要对其代码写法有一定了解, 通过查看资料,觉得Intent这个对象在里面起到很大的作用,所以想通过熟悉这个对象,进而Android入门



实验设计:

实现button点击之后的Activity(页面跳转)

从MainActivity, 跳转到Activity1, 同时传递mainPageInfo值


步骤:

新建一个Android项目, 名为AndroidTestProject

创建主页面为MainActivity,

1)在它的onCreate函数里加入一个button, 名为 toActivity1Btn

但是在这一步之前, 需要写到main_acitvity.xml中添加这个button

main_activity.xml

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

    <Button
        android:id="@+id/toActivity1_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="17dp"
        android:text="@string/toActivity1_btn" />

</RelativeLayout>


2)这是发现最后一行

android:text="@string/toActivity1_btn" />
这么写是为了方便统一管理命名方式,

所以还需要到string.xml中注册这个toActivity1_btn的名字

string.xml(后面还要修改这个文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidTestProject</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="toActivity1_btn">Jump To Activity1</string>

</resources>

于是添加了上面string.xml文件中的toActivity1_btn的命名


3) 在MainActivity.java中实际调用toActivity1_btn, 并且加入页面跳转的click事件

package atp.ui;

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

public class MainActivity extends Activity {

	private Button toActivity1_btn = null; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		toActivity1_btn = (Button)findViewById(R.id.toActivity1_btn);
		toActivity1_btn.setOnClickListener(new toActivity1BtnClickListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_activity, menu);
		return true;
	}

	private class toActivity1BtnClickListener implements OnClickListener{
		@Override
		public void onClick(View arg0) {
			//create intent 
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, Activity1.class);

			//create a bundle, then put it into intent
			Bundle bundle = new Bundle();
			bundle.putString("mainPageInfo", "Hello Activity1"); // 压入数据
			intent.putExtras(bundle);
			
			//use intent to define the page direction
			MainActivity.this.startActivity(intent);
		}

	}

}



4)创建Activity1, 用来作为上面三个步骤的跳转页面

同时在activity1.xml(即Activity1.java所对应的页面文件)加入一个textView用来输出刚才从MainActivity中传来的mainPageInfo的值

activity1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView_activity1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/textView_activity1" >
    </TextView>

</LinearLayout>


Activity1.java

package atp.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class Activity1 extends Activity {
	private TextView textView_activity1 = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity1);
		Bundle bundleFromMainActivity = this.getIntent().getExtras();
		textView_activity1 = (TextView)findViewById(R.id.textView_activity1);
		String mainPageInfo = bundleFromMainActivity.getString("mainPageInfo");
		textView_activity1.append(mainPageInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity1, menu);
		return true;
	}

}

strings.xml 中再加入textView的string值

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidTestProject</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="toActivity1_btn">Jump To Activity1</string>
    <string name="title_activity1">Activity1</string>
    <string name="textView_activity1">Show Info From MainActivity: </string>

</resources>

5) 千万不能遗忘的在AndroidManifest.xml 中需要注册这个activity, 路径很重要

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

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

    <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
            android:name=".Activity1"
            android:label="@string/title_activity1" >
        </activity>
    </application>

</manifest>


完成之后的效果图:

MainActivity



Activity1






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值