一 、显示启动
1、在应用中增加一个Activity,这需要修改AndroidManifest.xml文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.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="com.example.test.NextActivity">
</activity>
</application>
2、在layout文件夹中新建两个.xml文件 activity_main.xml 和 activity_next.xml,并在xml中定义相应的跳转按钮。
3、new class MainActivity.java
package com.example.asd;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button fistButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fistButton = (Button) findViewById(R.id.fistButton);
fistButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent();
it.setClass(MainActivity.this, NextActivity.class);
startActivity(it);
MainActivity.this.finish();
}
});
}
}
new class NextActivity.java
package com.example.asd;
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;
public class NextActivity extends Activity {
private Button nextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(NextActivity.this, MainActivity.class);
startActivity(intent);
NextActivity.this.finish();
}
});
}
}
二、隐式启动
1、在应用中增加一个Activity,这需要修改AndroidManifest.xml文件 ,与显示的不同在于,添加了<intent-filter></intent-filter>
<activity
android:name="com.example.test.NextActivity">
<intent-filter >
<action android:name="qwe"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
2、鼠标响应
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* Intent intent = new Intent();
intent.setClass(MainActivity.this,NextActivity.class);*/
Intent intent=new Intent("qwe");
startActivity(intent);
MainActivity.this.finish();
}
});