Intent在活动中穿梭

Intent在活动中穿梭

简单一个活动到另一个活动:

首先创建两个活动,每个含有一个按键。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity"
>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_1"
        android:text="button_1"
/>

</android.support.constraint.ConstraintLayout>

activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".Main2Activity"
>

    <Button
        android:id="@+id/button_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="button_2"
        app:layout_constraintBaseline_toBaselineOf="@+id/button_3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="button_3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
/>

    <Button
        android:id="@+id/button_4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="button_4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_2"
/>

</android.support.constraint.ConstraintLayout>

Java

package com.example.myapplication2;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });
    }
}

点击button1即可跳转到活动2

Intent的隐式用法,可以跳转到内部活动和外部活动比如:打电话、百度

package com.example.myapplication2;

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

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button button4 = (Button) findViewById(R.id.button_4);//设置触发点
        
Button button3 = (Button) findViewById(R.id.button_3);//设置触发点
        
button4.setOnClickListener(new View.OnClickListener() {//设置监听器
            
@Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);//制定内置动作为ACTION_DIAL
                
intent.setData(Uri.parse("tel:10086"));//指定协议和电话号码
                
startActivity(intent);//启动intent
            
}
        });

        button3.setOnClickListener(new View.OnClickListener() {//设置监听器
            
@Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);//制定内置动作为ACTION_VIEW
                
intent.setData(Uri.parse("http://www.baidu.com"));//parse()解析字符串为uri地址然后setData()
                // 方法传递数值
                
startActivity(intent);//启动intent
            
}
        });
    }
}

Intent向下一个活动传值

MainActivity:

package com.example.myapplication2;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("key", "value");//putExtra()方法传值,key-value形式
                
startActivity(intent);
            }
        });


    }
}

Main2Activity:

package com.example.myapplication2;

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

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Intent intent = getIntent();//获取Main2Activityintent
        
String getString = intent.getStringExtra("key");//通过getStringExtra()方法得到value
        
Log.d("Main2Activity", getString);//打印
}
    
}

Intent向上一个活动传值

向下一个活动传值都有一个开始intent的方法startActivity(),而向上一个活动传值则是直接按back键,但是活动也被销毁了。于是还有一个方法startActivityForResult()可以在销毁活动的时候向上一个活动返回一个结果。

 

把上面的startActivity(intent);换成startActivityForResult(intent, 1);//参数:intent,请求码迪奥调用startActivityForResult()到达第二个活动,此时第二个活动只需要重写按下back键返回的方法onBackPressed(),设置一个值然后加一个setResult()方法把值传递回去就好,

@Override

Public void onBackPressed() {

Intent intent = new Intent();

Intent.setPutExter(key1, value1);

setResult(RESULT_OK, intent);//两个参数:RESULT_OK/RESULT_CANCELED(处理结果其实就是判定用),intent对象

 

}

但是back到第一个活动的时候会回调一个onActivityResult()方法,代码如下:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1:
            if(resultCode == RESULT_OK) {
                String returnString = data.getStringExtra("key");
                Log.d("MainActivity", returnString);
            }
            break;
            default:

    }
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值