1 需求

需求1:显式Intent实现Activity跳转


2 接口 

Intent  |  Android Developers

ComponentName  |  Android Developers


3 示例代码

strings.xml

<resources>
    <string name="app_name">LearnIntentExplict</string>
    <string name="method1">public Intent(Context packageContext, Class&amp;lt;?&amp;gt; cls)</string>
    <string name="method2">public Intent(String action, Uri uri, Context packageContext, Class&amp;lt;?&amp;gt; cls)</string>
    <string name="method3">public Intent setClass(@NonNull Context packageContext, @NonNull Class&amp;lt;?&amp;gt; cls)</string>
    <string name="method4">public Intent setClassName(@NonNull String packageName, @NonNull String className)</string>
    <string name="method5">public Intent setClassName(@NonNull Context packageContext, @NonNull String className)</string>
    <string name="method6">public ComponentName(@NonNull String pkg, @NonNull String cls)</string>
    <string name="method7">public ComponentName(@NonNull Context pkg, @NonNull String cls)</string>
    <string name="method8">public ComponentName(@NonNull Context pkg, @NonNull Class&lt;?&gt; cls)</string>
    <string name="method9">public ComponentName (Parcel in)</string>
</resources>

MainActivity.java

package com.example.learnintentexplict;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button btn_explict_1 = findViewById(R.id.btn_explict_1);
        Button btn_explict_2 = findViewById(R.id.btn_explict_2);
        Button btn_explict_3 = findViewById(R.id.btn_explict_3);
        Button btn_explict_4 = findViewById(R.id.btn_explict_4);
        Button btn_explict_5 = findViewById(R.id.btn_explict_5);
        Button btn_explict_6 = findViewById(R.id.btn_explict_6);
        Button btn_explict_7 = findViewById(R.id.btn_explict_7);
        Button btn_explict_8 = findViewById(R.id.btn_explict_8);
        Button btn_explict_9 = findViewById(R.id.btn_explict_9);

        btn_explict_1.setOnClickListener(this);
        btn_explict_2.setOnClickListener(this);
        btn_explict_3.setOnClickListener(this);
        btn_explict_4.setOnClickListener(this);
        btn_explict_5.setOnClickListener(this);
        btn_explict_6.setOnClickListener(this);
        btn_explict_7.setOnClickListener(this);
        btn_explict_8.setOnClickListener(this);
        btn_explict_9.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_explict_1:
                onClickButton1(view);
                break;
            case R.id.btn_explict_2:
                onClickButton2(view);
                break;
            case R.id.btn_explict_3:
                onClickButton3(view);
                break;
            case R.id.btn_explict_4:
                onClickButton4(view);
                break;
            case R.id.btn_explict_5:
                onClickButton5(view);
                break;
            case R.id.btn_explict_6:
                onClickButton6(view);
                break;
            case R.id.btn_explict_7:
                onClickButton7(view);
                break;
            case R.id.btn_explict_8:
                onClickButton8(view);
                break;
            case R.id.btn_explict_9:
                onClickButton9(view);
                break;
            default:
                break;
        }
    }

    private void onClickButton1(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    private void onClickButton2(View view) {
        Toast.makeText(this, "方案二未实现", Toast.LENGTH_SHORT).show();
    }

    private void onClickButton3(View view) {
        Intent intent = new Intent();
        intent.setClass(this, SecondActivity.class);
        startActivity(intent);
    }

    private void onClickButton4(View view) {
        Intent intent = new Intent();
        intent.setClassName("com.example.learnintentexplict", "com.example.learnintentexplict.SecondActivity");
        startActivity(intent);
    }

    private void onClickButton5(View view) {
        Intent intent = new Intent();
        intent.setClassName(this, "com.example.learnintentexplict.SecondActivity");
        startActivity(intent);
    }

    private void onClickButton6(View view) {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.example.learnintentexplict", "com.example.learnintentexplict.SecondActivity");
        intent.setComponent(componentName);
        startActivity(intent);
    }

    private void onClickButton7(View view) {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName(this, "com.example.learnintentexplict.SecondActivity");
        intent.setComponent(componentName);
        startActivity(intent);
    }

    private void onClickButton8(View view) {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName(this, SecondActivity.class);
        intent.setComponent(componentName);
        startActivity(intent);
    }

    private void onClickButton9(View view) {
        Toast.makeText(this, "方案九未实现", Toast.LENGTH_SHORT).show();
    }

}

4 参考资料

原文地址:https://blog.csdn.net/pwp032984/article/details/122853299

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_41182.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注