多种方式实现页面切换
今天老师留的作业如题,要求用三种方式实现:按钮切换,按键切换和触摸切换。
先说我做的第一种方式逻辑:
先上代码:
OneActivity.java文件代码:
package cn.class3g;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class OneActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button nextButton = (Button)findViewById(R.id.next);
nextButton.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
setContentView(R.layout.two);
ButtonupButton=(Button)findViewById(R.id.up);
upButton.setOnClickListener(newOnClickListener() {
@Override
publicvoid onClick(View v) {
setContentView(R.layout.main);
ButtonnextButton = (Button)findViewById(R.id.next);
}
});
}
});
}
}
解释:这是我最初写的代码,布局文件写了两个:main.xml和two.xml,分别显示两个页面,分别有一个
Main.xml代码:
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="这是第一页" />