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文件和布局比较简单,代码这里就没有贴出