【Android之本菜鸡的毕设】第三节 Intent使用及实现页面跳转与互相传值

0.设想

  • 实现Intent实现页面跳转的几种方法;
  • 并且测试一下Intent拨号和开启浏览器的方法。

1.啥是Intent呀?

  • Intent被翻译成意图、信使;
  • 负责在组件之间传递动作和数据;

2.Intent实现页面跳转与传值

参考https://blog.youkuaiyun.com/u013969018/article/details/79834175
2.1主界面MainActivity跳转到界面A(ActivityA)
MainActivity.java

package com.yh.intenttest;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button switch_bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    //组件初始化
    private void initView(){
        switch_bt1 = findViewById(R.id.switch_bt1);
        switch_bt1.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.switch_bt1:
                Intent intent1 = new Intent(MainActivity.this,ActivityA.class);
                startActivity(intent1);
                break;
            default:
                break;

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前为主界面MainActivity"
            android:layout_above="@+id/switch_bt1"
            android:layout_marginBottom="40dp"
            android:textSize="30sp"
            />

        <Button
            android:id="@+id/switch_bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转到界面A"
            android:textSize="30sp"
            />
    </LinearLayout>

</RelativeLayout>

ActivityA.java

package com.yh.intenttest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ActivityA extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
    }
}

activity_a.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".ActivityA">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是ActivityA——界面A"
        android:textSize="30sp"
        />
</RelativeLayout>

运行结果:
intent1
intent2
2.2主界面MainActivity跳转到界面B(ActivityB),并向界面B传值

  • 方法1:附加数据到Intent对象上
    MainActivity.java
package com.yh.intenttest;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button switch_bt1;
    private Button switch_bt2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    //组件初始化
    private void initView(){
        switch_bt1 = findViewById(R.id.switch_bt1);
        switch_bt1.setOnClickListener(this);
        switch_bt2 = findViewById(R.id.switch_bt2);
        switch_bt2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.switch_bt1:
                Intent intent1 = new Intent(MainActivity.this,ActivityA.class);
                startActivity(intent1);
                break;
                case R.id.switch_bt2:
                    Intent intent2 = new Intent(MainActivity.this,ActivityB.class);
                    intent2.putExtra("ActivityName","我是MainActivity!");
                    intent2.putExtra("Data",18);
                    startActivity(intent2);
                    break;
            default:
                break;

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前为主界面MainActivity"
            android:layout_above="@+id/switch_bt1"
            android:layout_marginBottom="40dp"
            android:textSize="30sp"
            />

        <Button
            android:id="@+id/switch_bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转到界面A"
            android:textSize="30sp"
            android:layout_marginBottom="30dp"
            />
        <Button
            android:id="@+id/switch_bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转并传数据到界面B"
            android:textSize="30sp"
            />
    </LinearLayout>

</RelativeLayout>

ActivityB.java

package com.yh.intenttest;

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

public class ActivityB extends AppCompatActivity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        initView();
    }
    private void initView(){
        textView = findViewById(R.id.switch_tv_b);
        Intent intent = getIntent();
        String activityName = intent.getStringExtra("ActivityName");
        int data = intent.getIntExtra("Data",0);
        textView.setText(activityName+"\n\r传来了数据:"+data);
    }
}

activity_b.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".ActivityB">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是ActivityB——界面B"
            android:textSize="30sp"
            android:layout_marginBottom="30dp"
            />
        <TextView
            android:id="@+id/switch_tv_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这里存储接收的数据信息"
            android:textSize="30sp"
            />
    </LinearLayout>
</RelativeLayout>

运行结果:
intent传数据
intent传数据

  • 方法2:使用Bundle捆绑数据到Intent上
    只需要修改Intent那部分:
    MainActivity.java
Intent intent2 = new Intent(MainActivity.this,ActivityB.class);
 /*
intent2.putExtra("ActivityName","我是MainActivity!");
intent2.putExtra("Data",18);
*/
Bundle bundle = new Bundle();
bundle.putString("ActivityName","我是MainActivity(Bundle)!");
bundle.putInt("Data",18);
intent2.putExtras(bundle);
startActivity(intent2);
break;

ActivityB.java

Intent intent = getIntent();
 /*String activityName = intent.getStringExtra("ActivityName");
 int data = intent.getIntExtra("Data",0);
 */
Bundle bundle = intent.getExtras();
String activityName = bundle.getString("ActivityName");
int data = bundle.getInt("Data");
textView.setText(activityName+"\n\r传来了数据:"+data);

运行结果没有问题:
Intent传数据Bundle
2.3主界面MainActivity跳转到界面C(ActivityC),向界面C传值并获得C传来的值
MainActivity.java

package com.yh.intenttest;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button switch_bt1;
    private Button switch_bt2;
    private Button switch_bt3;
    private TextView switch_tv_main;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    //组件初始化
    private void initView(){
        switch_bt1 = findViewById(R.id.switch_bt1);
        switch_bt1.setOnClickListener(this);
        switch_bt2 = findViewById(R.id.switch_bt2);
        switch_bt2.setOnClickListener(this);
        switch_bt3 = findViewById(R.id.switch_bt3);
        switch_bt3.setOnClickListener(this);
        switch_tv_main = findViewById(R.id.switch_tv_main);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.switch_bt1:
                Intent intent1 = new Intent(MainActivity.this,ActivityA.class);
                startActivity(intent1);
                break;
                case R.id.switch_bt2:
                    Intent intent2 = new Intent(MainActivity.this,ActivityB.class);
                    /*
                    intent2.putExtra("ActivityName","我是MainActivity!");
                    intent2.putExtra("Data",18);
                     */
                    Bundle bundle = new Bundle();
                    bundle.putString("ActivityName","我是MainActivity(Bundle)!");
                    bundle.putInt("Data",18);
                    intent2.putExtras(bundle);
                    startActivity(intent2);
                    break;
            case R.id.switch_bt3:
                Intent intent3 = new Intent(MainActivity.this,ActivityC.class);
                startActivityForResult(intent3,10001);
                break;
            default:
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==10001&&resultCode==10002){
            String activityName = data.getStringExtra("BackInfo");
            int backData = data.getIntExtra("Data",0);
            switch_tv_main.setText(activityName+"\n\r回传了数据:"+backData);
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="当前为主界面MainActivity"
            android:layout_above="@+id/switch_bt1"
            android:layout_marginBottom="40dp"
            android:textSize="30sp"
            />

        <Button
            android:id="@+id/switch_bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转到界面A"
            android:textSize="30sp"
            android:layout_marginBottom="30dp"
            />
        <Button
            android:id="@+id/switch_bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转并传数据到界面B"
            android:layout_marginBottom="30dp"
            android:textSize="30sp"
            />
        <Button
            android:id="@+id/switch_bt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转并传数据到界面C"
            android:layout_marginBottom="30dp"
            android:textSize="30sp"
            />
        <TextView
            android:id="@+id/switch_tv_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这里存放界面C回传的数据"
            android:textSize="30sp"
            />
    </LinearLayout>
</RelativeLayout>

ActivityC.java

package com.yh.intenttest;

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

public class ActivityC extends AppCompatActivity implements View.OnClickListener{
    private Button bt_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
        initView();
    }
    private void initView(){
        bt_back = findViewById(R.id.back_bt_c);
        bt_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_bt_c:
                Intent intent = new Intent(ActivityC.this,MainActivity.class);
                intent.putExtra("BackInfo","我是ActivityC");
                intent.putExtra("Data",19150307);
                setResult(10002,intent);
                finish();
                break;
        }
    }
}

activity_c.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".ActivityB">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是ActivityC——界面C"
            android:textSize="30sp"
            android:layout_marginBottom="30dp"
            />

        <Button
            android:id="@+id/back_bt_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回原界面并回传数据"
            android:textSize="30sp"
            />
    </LinearLayout>
</RelativeLayout>

运行情况:
IntentForResult
IntentForResult
IntentForResult

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值