Pass data through Intent

Simple data types and serialized objects can be saved in the Intent object through the putExtra method in the Intent class, and then the corresponding data can be retrieved using the getXxx method in the target Activity .

It mainly includes the following contents: MainActivity.java, OtherActivity.java (new java class based on Android Activity), factivity_main.xml, other.xml , AndroidMainFest.xml

MainActivity.java

package com.example.pro4;


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.widget.Button;
public class MainActivity extends Activity {
private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载布局文件
        button=(Button)this.findViewById(R.id.button);//
        button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Trigger an intent when the button is clicked
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
// pass the data in the intent
intent.putExtra("name", "Zhang San");
intent.putExtra("age",23);
intent.putExtra("address", "Beijing");
//start intent
startActivity(intent);
}
});
    }


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


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


OtherActivity.java

package com.example.pro4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class OtherActivity extends Activity {


private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg);
setContentView(R.layout.other);  
        textView = (TextView)this.findViewById(R.id.msg);//Get text box  
        Intent intent = getIntent();//Get intent object  
        Integer age = intent.getIntExtra(" age", 0);//Get the intent object data  
        String name = intent.getStringExtra("name"); //Get the intent object data String  
        address = intent.getStringExtra("address");//Get the intent object data  
        textView. setText("age--->>"+age+"\n"+"name--->>"+name+"\n"+"address--->>"+address);//Set to the text box

}


}

Layout file layout under res

factivity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pro4.MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
        <Button android:id="@+id/button" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:text="test use Intent Data!"></Button>


</RelativeLayout>

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:id="@+id/msg"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" >  
    </TextView>  
</LinearLayout>


AndroidMainFest.xml

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


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


    <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"></activity>  
    </application>


</manifest>

Guess you like

Origin blog.csdn.net/u012388338/article/details/48553153