Android Studio [页面的跳转和传值]

AActivity.java

package com.xdw.a122.jump;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xdw.a122.R;

public class AActivity extends AppCompatActivity {
    private Button mBtnJump;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        mBtnJump=findViewById(R.id.btn_jump1);
        mBtnJump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显式1
                Intent intent=new Intent(AActivity.this,BActivity.class);
                Bundle bundle = new Bundle();           //为bundle分配
                bundle.putString("name","Bollen");     //转入bundle
                bundle.putInt("number",250);            //转入bundle
                intent.putExtras(bundle);  //传输bundle--intent
//                startActivity(intent);         //执行
                startActivityForResult(intent,0);  //执行 并返回一个结果 请求码0
                //显式2
//                Intent intent=new Intent();
//                intent.setClass(AActivity.this,BActivity.class);
//                startActivity(intent);
                //显式3
//                Intent intent=new Intent();
//                intent.setClassName(AActivity.this,"com.xdw.a122.jump.BActivity");
//                startActivity(intent);
                //显式4
//                Intent intent=new Intent();
//                intent.setComponent(new ComponentName(AActivity.this,BActivity.class));
//                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(AActivity.this,data.getExtras().getString("title"),Toast.LENGTH_LONG).show();
    }
}

BActivity.java

package com.xdw.a122.jump;

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

import com.xdw.a122.R;

public class BActivity extends AppCompatActivity {
    private TextView mTvTitle;
    private Button mBtnFinish;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        mTvTitle=findViewById(R.id.tv_title);
        mBtnFinish=findViewById(R.id.btn_finish);
        final Bundle bundle=getIntent().getExtras();    //接收Extras
        String name=bundle.getString("name");
        int number=bundle.getInt("number");
        mTvTitle.setText(name+","+number);  //显示传递值
        mBtnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();         //为返回值bundler1分配空间
                Bundle bundle1=new Bundle();
                bundle1.putString("title","我回来了");
                intent.putExtras(bundle1);
                setResult(AActivity.RESULT_OK,intent);      //返回ok-1
                finish();           //关闭页面
            }
        });
    }
}

activity_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:gravity="center_horizontal"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_jump1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@drawable/bg_btn3"
        android:text="Jump"/>
</LinearLayout>

activity_b.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"/>
    <Button
        android:id="@+id/btn_finish"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="返回"
        android:textSize="20sp"
        android:background="@drawable/bg_btn3"/>
</LinearLayout>

 

Android Studio [页面的跳转和传值]

上一篇:【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user


下一篇:Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询