• android组件通讯 IntentAction属性


    Action属性应用实例

    1、自定义Action属性

    程序文件

    /Chapter06_Intent_TestAction/src/com/amaker/ch06/app/MainActivity.java

    代码
    package com.amaker.ch06.app;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    /**
    *
    * 测试Intent Action 属性
    */
    public class MainActivity extends Activity {
    // 定义Action 属性常量
    public static final String MY_ACTION="com.amaker.ch07.app.MY_ACTION";
    // 声明Button
    private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置布局视图
    setContentView(R.layout.main);
    // 实例化Button
    btn = (Button)findViewById(R.id.Button01);
    btn.setOnClickListener(
    new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 实例化Intent
    Intent intent = new Intent();
    // 为Intent设置Action属性
    intent.setAction(MY_ACTION);
    // 启动Activity
    startActivity(intent);
    }
    });
    }
    }

    /Chapter06_Intent_TestAction/src/com/amaker/ch06/app/MyActivity.java

    代码
    package com.amaker.ch06.app;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    /**
    * 测试Intent Action 属性
    */
    public class MyActivity extends Activity {
    // 声明TextView
    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置视图布局
    setContentView(R.layout.my_layout);
    // 获得Intent对象
    Intent intent = getIntent();
    // 获得Action
    String action = intent.getAction();
    // 获得TextView
    tv = (TextView)findViewById(R.id.TextView01);
    // 设置内容
    tv.setText(action);
    }
    }

    布局文件

    /Chapter06_Intent_TestAction/res/layout/main.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    >

    <Button
    android:text="测试Intent的Action属性"
    android:id
    ="@+id/Button01"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>

    </LinearLayout>

    /Chapter06_Intent_TestAction/res/layout/my_layout.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    >

    <TextView
    android:text="@+id/TextView01"
    android:id
    ="@+id/TextView01"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></TextView>

    </LinearLayout>

    清单文件

    /Chapter06_Intent_TestAction/AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.amaker.ch06.app"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".MainActivity"
    android:label
    ="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <activity android:name="MyActivity">
    <intent-filter>
    <action android:name="com.amaker.ch06.app.MY_ACTION" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

    </manifest>

    2、访问系统的Action属性

    程序文件

    /Chapter06_Intent_TestAction2/src/com/amaker/ch06/app/MainActivity.java

    代码
    package com.amaker.ch06.app;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    /**
    *
    * 测试Intent Action 属性
    */
    public class MainActivity extends Activity {
    // 声明Button
    private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置当前布局视图
    setContentView(R.layout.main);
    // 实例化Button
    btn = (Button)findViewById(R.id.Button01);
    btn.setOnClickListener(
    new OnClickListener() {
    @Override
    public void onClick(View v) {
    // 创建Intent
    Intent intent = new Intent();
    // 设置Intent Action属性
    intent.setAction(Intent.ACTION_GET_CONTENT);
    // 设置Intent Type 属性
    intent.setType("vnd.android.cursor.item/phone");
    // 启动Activity
    startActivity(intent);
    }
    });
    }
    }

    布局文件

    /Chapter06_Intent_TestAction2/res/layout/main.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    >

    <Button
    android:text="测试Intent的 Action属性"
    android:id
    ="@+id/Button01"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"></Button>

    </LinearLayout>

    清单文件

    /Chapter06_Intent_TestAction2/AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.amaker.ch06.app"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
    android:label
    ="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

    </manifest>
  • 相关阅读:
    docker部署项目,对镜像,容器的操作
    技术汇总:第十六章:关于登录与退出的token
    关于EZDML数据库表结构制作设计工具使用踩的坑
    技术汇总:第十七章:支付宝对接公钥,私钥
    集成Mybatis
    解决errorCode 0, state 08001 报错
    Spring缓存注解@Cacheable,@CachePut , @CacheEvict
    解决:Could not initialize class org.hibernate.validator.internal.engine.Configura
    @ConditionalOnMissingBean注解的作用
    解决:WARNING: An illegal reflective access operation has occurred
  • 原文地址:https://www.cnblogs.com/linzheng/p/1939727.html
Copyright © 2020-2023  润新知