android_putExtra与bundle

第一个页面

layout

只有两个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_third"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.iamchan.framelay.ThirdActivity">
    <Button
        android:text="按钮"
        android:id="@+id/btn_sub"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <Button
        android:text="按钮2"
        android:id="@+id/btn_sub2"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
</LinearLayout>

java

package com.example.iamchan.framelay;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ThirdActivity extends AppCompatActivity {


    private Button btn_sub;
    private Button btn_sub2;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        btn_sub= (Button) findViewById(R.id.btn_sub);
        btn_sub2= (Button) findViewById(R.id.btn_sub2);
        /*
        *
        * 简单的传值
        *
        * */
       /* btn_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(ThirdActivity.this,FouthActivity.class);
                intent.putExtra("name","iamchan");
                intent.putExtra("age","保密");
                intent.putExtra("number",123456);
                startActivity(intent);
            }
        });*/
        /*
        *
        * bundle使用起来比较方便
        * 相同数据实例化一个bundle可以在不同页面传输
        * 我现在要从third界面 跳转到fouth界面或者sixth界面 需要写2个Intent 如果你还要涉及的传值的话
        * 你的Intent就要写两遍添加值的方法 (putExtra)我用1个Bundle把值存在里面 跳转时放入intent中

        * 如果我现在有三个页面
        * 跳转时从第一个页面用bundle传值跳转到第二个页面
        * 第二页面传相同的值到第三个页面 就可以直接传上一页面的bundle就行 参照页面二和三
        *
        * */
        /*bundle=new Bundle();
        bundle.putString("name","iamchan");
        bundle.putString("age","保密");
        bundle.putInt("number",123456);*/

       /* btn_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(ThirdActivity.this,FouthActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        btn_sub2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(ThirdActivity.this,SixthActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });*/

    }
}

第二个页面

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_fouth"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.iamchan.framelay.FouthActivity">
    <TextView
        android:text=""
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_intent"
        android:text="跳转"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
</LinearLayout>

java

package com.example.iamchan.framelay;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FouthActivity extends AppCompatActivity {


    private TextView tv_text;
    private Button btn_intent;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fouth);
        tv_text= (TextView) findViewById(R.id.tv_text);
        btn_intent= (Button) findViewById(R.id.btn_intent);


        Intent intent=getIntent();
        /*
        *
        * intent 获取extra
        *
        * */
       /* if(intent!=null){
            String name=intent.getStringExtra("name");
            String age=intent.getStringExtra("age");
            int number=intent.getIntExtra("number",0);
            tv_text.setText("名字:"+name+"年龄:"+age+"编号"+number);
        }*/

        /*if(intent!=null){
            bundle=intent.getExtras();
            String name=bundle.getString("name");
            String age=bundle.getString("age");
            int number=bundle.getInt("number",0);
            tv_text.setText("名字:"+name+"年龄:"+age+"编号"+number);
        }*/


        /*
        *
        * 直接拿到bundle传给下一个页面
        *
        * */
       /* btn_intent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(FouthActivity.this,SixthActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });*/

    }
}

第三个页面

layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_sixth"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.iamchan.framelay.SixthActivity">
    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

java

package com.example.iamchan.framelay;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SixthActivity extends AppCompatActivity {


    private TextView tv_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sixth);
        tv_text= (TextView) findViewById(R.id.tv_text);

        /*Intent intent=getIntent();
        if(intent!=null){
            Bundle bundle=intent.getExtras();
            String name=bundle.getString("name");
            String age=bundle.getString("age");
            int number=bundle.getInt("number",0);
            tv_text.setText("名字:"+name+"年龄:"+age+"编号"+number);
        }*/
    }
}

猜你喜欢

转载自blog.csdn.net/iamchan/article/details/82787108
今日推荐