• Android基础——intent的ComponentName


    intent通过ComponentName指定要执行的组件名字,创建一个Activity时就可以用这个方式进行指定

    通过Main启动Detail

    两个活动的布局

    Detail

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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=".DetailActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="详细内容"/>
    
    </LinearLayout>

    Main

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="详细信息"/>
    
    </LinearLayout>

    java调用代码

    package com.example.myintenti;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.view.KeyEventDispatcher;
    
    import android.content.ComponentName;
    import android.content.Intent;
    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);
    
            //ComponentName属性:要启动的组件名字
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override//使用ComponentName属性创建一个活动
                public void onClick(View v) {
                    Intent intent = new Intent();
                    ComponentName componentName = new ComponentName(
                            "com.example.mtintenti","com.example.mtintenti.DetailActivity"
                    );
                    intent.setComponent(componentName);
                    startActivity(intent);
                }
            });
        }
    }
  • 相关阅读:
    使用SQL查询所有数据库名和表名
    vue打包时给静态资源增加版本号
    mac笔记本好用的快捷键汇总
    jquery项目好用的插件汇总
    通过js禁止输入空格(试用场景:当用字符串拼接插入dom节点时,onkeyup这些方法都不好使可用这个)
    textarea和type=number输入去空格限制字数问题
    用websocket建立远程连接(vue)
    配置本地服务器
    webpack打包路径问题
    序列号和反序列化==》nodejs之querystring模块(尼玛,太强大,好用耶)
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12236837.html
Copyright © 2020-2023  润新知