Android学习笔记——Activity之间的跳转(五)

1:使用Intent(意图)的方式实现Activity跳转

(1)MainActivity.java:

public class MainActivity extends Activity {
	private Button startOther;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startOther=(Button) findViewById(R.id.button);
		startOther.setOnClickListener(new StartListener());
	}

	class StartListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// 当点击事件触发后执行,启动OtherActivity
			//创建一个Intent对象
			Intent intent=new Intent();
			//调用setClass方法指定启动某一个Activity
			intent.setClass(MainActivity.this, OtherActivity.class);
			//调用startActivity
			startActivity(intent);
			
		}
		
	}
}

(2)OtherActivity.java
//创建一个类继承自Activity
public class OtherActivity extends Activity {
	
	@Override
	//重写onCreate方法,当前Activity的入口
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_other);
	}
}

(3)activity_main.xml
<LinearLayout 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:text="第一个Activity" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="启动OtherActivity"/>

</LinearLayout>

(4)activity_other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第二个Activity"/>

</LinearLayout>

2:BackStack回退栈(先进后出)

猜你喜欢

转载自my.oschina.net/u/3734228/blog/2873085