zoukankan      html  css  js  c++  java
  • 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);
                }
            });
        }
    }
  • 相关阅读:
    不参加IT培训,如何通过自学的方式成功转行?(蜗牛学院)
    惠普电脑win10关闭自动调节亮度
    原生Ajax发送get、post请求每一步
    HTML5的web 存储localStorage、sessionStorage
    node + multer存储element-ui上传的图片
    html块级元素的水平垂、直居中的方式
    vuex之Mutation(三)
    mint ui的tabBar监听路由变化实现tabBar切换
    Vue使用better-scroll左右菜单联动
    vuex之getter(二)
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12236837.html
Copyright © 2011-2022 走看看