Intent跳转布局及传递数据

来源:互联网 发布:金融数据公司 编辑:程序博客网 时间:2024/05/19 14:17

首先在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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:gravity="center"><TextViewandroid: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{@Overrideprotected 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><activityandroid: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跳转到OtherActivityIntent 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;@Overrideprotected 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); //如果没有年龄这个参数,就返回1text.setText("姓名:"+name+"\n年龄:"+age);}}

效果图:




原创粉丝点击