第十三章,隐式intent小demo(Android)

MainActivity.java

package com.example.demo16;

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

public class MainActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 去掉Activity的标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		// 注释原来布局
		// setContentView(R.layout.activity_main);

		// 实例化布局
		LinearLayout llayout = new LinearLayout(this);
		// 布局方向
		llayout.setOrientation(LinearLayout.VERTICAL);
		// 添加到窗口中
		setContentView(llayout);
		// 实例化按钮
		Button b = new Button(this);
		b.setOnClickListener(this);
		// 把按钮添加到布局中
		llayout.addView(b);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent intent = new Intent("myaction");
		intent.addCategory("mycategory");
		startActivity(intent);

	}

}
AndroidManifest.xml

在文件中添加第二个Activity那段内容即可

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demo16.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 start -->
        <activity android:name="com.example.demo16.SecondActivity" >
            <intent-filter>
                <action android:name="myaction" />


                <category android:name="mycategory" />

                <!-- 这行表示是默认的category,一定要写,如果没有mycategory,这一行也要写 -->
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <!-- 这是第二个Activity end -->
    </application>

运行截图:


点击按钮跳转到


注:因为SecondActivity文件和布局比较简单,代码这里就没有贴出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值