Intent跳转布局及传递数据

首先在mian.xml中添加按钮控件,我需要点击按钮就能从一个Activity跳转到另一个Activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    >

    <Button
        android:text="跳转"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:onClick="buttonListener"/>

</LinearLayout>

添加xml文件,命名为other,在里面添加TextView控件:

<?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"
	android:gravity="center">

	<TextView
		android:layout_height="wrap_content"
		android:text="另一个Activity"
		android:layout_width="wrap_content"
		android:id="@+id/otherTextView"/>

</LinearLayout>


添加java文件,命名OtherActivity:

package com.intent1;

import android.app.*;
import android.os.*;

public class OtherActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
	}
}


在AndroidManifest.xml中注册OtherActivity:

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

    <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=".OtherActivity"/>
    </application>

</manifest>


在MainActivity.java中:

package com.intent1;

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
	public void buttonListener(View view)
	{
		//声明intent变量,从MainActivity跳转到OtherActivity
		Intent intent = new Intent(MainActivity.this, OtherActivity.class);
		//传递姓名和年龄
		intent.putExtra("姓名", "小明");
		intent.putExtra("年龄", 20);
		startActivity(intent);
	}
}


在OtherActivity.java中:

package com.intent1;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;

public class OtherActivity extends Activity
{
	//声明变量
	private TextView text;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		
		text = (TextView)findViewById(R.id.otherTextView);
		
		Intent intent = getIntent();
		//获取姓名和年龄
		String name = intent.getStringExtra("姓名");
		int age = intent.getIntExtra("年龄", 1); //如果没有年龄这个参数,就返回1
		
		text.setText("姓名:"+name+"\n年龄:"+age);
	}
}

效果图:




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值