• Activity的跳转和数据传递


    跳转

    1、显示跳转

    4种方法

    public class AActivity extends AppCompatActivity {
    
        private Button ma;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_a);
    
            ma=findViewById(R.id.btn_a);
            ma.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    //显式方法1
                    Intent intent=new Intent(AActivity.this,BActivity.class);
                    startActivity(intent);        
    
                    /*显式跳转方法2
                     Intent intent=new Intent();
                    intent.setClass(AActivity.this,BActivity.class);
                    startActivity(intent);
                     */
    
                    /*显式跳转方法3
                    Intent intent=new Intent();
                    intent.setClassName(AActivity.this,"com.example.textview.jump.BActivity");
                    startActivity(intent);
                     */
    
                    /*显式跳转方法4
                    Intent intent=new Intent();
                    intent.setComponent(new ComponentName(AActivity.this,"com.example.textview.jump.BActivity"));
                    startActivity(intent);
                     */
    
                }
            });
        }
    } 

    数据传递

    AActivity点击跳转后发送数据

     package com.example.textview.jump;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    import com.example.textview.R;
    
    public class AActivity extends AppCompatActivity {
    
        private Button ma;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_a);
    
            ma=findViewById(R.id.btn_a);
            ma.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    //显式方法1
                    Intent intent=new Intent(AActivity.this,BActivity.class);
    
                    //数值传递
                    Bundle bundle=new Bundle();
                    bundle.putString("name","刘亦菲");//bundle里放传输的数据
                    bundle.putInt("number",28);
                    intent.putExtras(bundle);//bundle通过intent传过去
                    startActivity(intent);
                }
            });
        }
    }
    

    BActivity接收数据
    B的布局界面放一个TextView用来显式传输来的文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:padding="10dp"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:textColor="#F08080"
            android:id="@+id/text_1"/>
    
    </LinearLayout>
    

    BActivity.java

    package com.example.textview.jump;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    import com.example.textview.R;
    
    public class BActivity extends AppCompatActivity {
    
        private TextView mTV;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_b);
    
            mTV=findViewById(R.id.text_1);
            Bundle bundle=getIntent().getExtras();
            String name=bundle.getString("name");
            int number=bundle.getInt("number");
            mTV.setText(name+","+number);
        }
    }
    

    startActivityForResul

    界面1跳转到界面2,界面2将得到的数据又转到界面1


    AActivity.java

    package com.example.textview.jump;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import com.example.textview.R;
    
    public class AActivity extends AppCompatActivity {
    
        private Button ma;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_a);
    
            ma=findViewById(R.id.btn_a);
            ma.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //显式方法1
                    Intent intent=new Intent(AActivity.this,BActivity.class);
                    //数值传递
                    Bundle bundle=new Bundle();
                    bundle.putString("name","刘亦菲");//bundle里放传输的数据
                    bundle.putInt("number",28);
                    intent.putExtras(bundle);//bundle通过intent传过去
                   // startActivity(intent);
                    startActivityForResult(intent,0);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Toast.makeText(this, data.getExtras().getString("title"), Toast.LENGTH_SHORT).show();
        }
    }
    

    BActivity.java

    package com.example.textview.jump;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import com.example.textview.R;
    
    public class BActivity extends AppCompatActivity {
    
        private TextView mTV;
        private Button mab;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_b);
    
            mTV=findViewById(R.id.text_1);
            mab=findViewById(R.id.ab_1);
            Bundle bundle=getIntent().getExtras();
            String name=bundle.getString("name");
            int number=bundle.getInt("number");
            mTV.setText(name+","+number);
    
            mab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    Bundle bundle1=new Bundle();
                    bundle1.putString("title","宝宝回啦了");
                    intent.putExtras(bundle1);
                    setResult(Activity.RESULT_OK,intent);
                    finish();
                }
            });
        }
    }
    

      

  • 相关阅读:
    网络运维架构
    Q in Q
    光纤/光模块的多模和单模
    Cisco VTP
    Cisco NTP配置
    惠普/aruba交换机
    【转】交换机背板带宽
    接入交换机下所有服务器不定时丢包
    windows 2012安装不了KB2919355
    Cisco交换机密码策略设置
  • 原文地址:https://www.cnblogs.com/ltw222/p/14903641.html
Copyright © 2020-2023  润新知