您的位置:首页 > 移动开发 > Android开发

android 关于页面跳转

2011-09-14 22:00 197 查看
首先介绍一种大家都不怎么使用的办法,那就是直接使用setContentView 方法代码如下
public class Mytest1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
setContentView(R.layout.main2);

}
});
}
}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<TextView android:text="TextView1" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Button1" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/editText1"
android:layout_width="match_parent" android:text="EditText1s"></EditText>
</LinearLayout>


main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<TextView android:text="TextView2" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/editText2"
android:layout_width="match_parent" android:text="EditText2"></EditText>
<Button android:text="Button2" android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


代码很简单,我也没有美化,有2个界面分别有一个textview,button,editview,界面启动时
setContentView(R.layout.main); 给按钮添加事件,点击事  setContentView(R.layout.main2);
这个方法很少用,不过有时候还是很有用的,因为这2个界面在一个类里面,共享所有变量和方法,对于一些传输数据很麻烦的有一定的帮助。缺点也很明显
如果跳转到复杂的页面就无能为力了,而且,在一个activity中,也没法返回,需要特殊处理。

然后是大家常用的利用intent对象跳转,这个就不多说了,给一个例子就可以了。布局文件仍然是上面的2个。
public class Mytest1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent=new Intent();
intent.setClass(Mytest1.this, MyTest2.class);
startActivity(intent);
}
});
}
}


public class MyTest2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button button = (Button) this.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(MyTest2.this, Mytest1.class);
startActivity(intent);
}
});
}
}


需要知道是,如果你在跳转之前调用了finish 就没法用返回键在返回了,实际上,返回键就是调用的finish。我的理解是没跳转一个界面都将先前的界面放到一个栈里面

而finish方法就是释放当前界面,那自然显示的是上个界面了。比如A跳转到B,这时候B调用finish就会到A 然后跳转到C,点返回键就会显示先前的A。




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: