activity_main.xml
创建一个Button按钮,点击启动监听事件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到HelloWorld" />
</LinearLayout>
MainActivity.java
获取Button界面控件,然后设置按钮监听方法,
通过intent.setClass方法设置要跳转的页面。
package com.example.intent2;
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;
/**
* 最简单的页面的跳转的实现
*
* @author Administrator
*
*/
public class MainActivity extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取Button按钮
button = (Button) findViewById(R.id.button1);
// 添加Button事件,当点击此按钮,进行页面的跳转
button.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent对象
Intent intent = new Intent();
// 设置要跳转的页面
intent.setClass(MainActivity.this, SecondActivity.class);
// 开始Activity
startActivity(intent);
}
};
}
SecondActivity.java
创建另一个Activity,点击按钮跳到这个activity对应的界面,即Hello,World页面,创建该Activity,自动生成布局文件activity_second.xml。
package com.example.intent2;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".SecondActivity" />
</RelativeLayout>
运行的效果:
点击跳转到HelloWorld按钮,
跳转到了hello,world布局文件中。