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。
相关文章推荐
- 【android】关于多个activity页面跳转,跳过中间页面返回前边页面传值
- 关于Android的Button响应页面跳转问题
- android关于crash时的提示信息,与页面跳转
- 关于在Android中Activity页面跳转的方法
- Android 关于Activity的跳转和finish时切换页面动画实现
- 关于android对页面栈的处理跳转
- 关于android对页面栈的处理跳转
- android 关于跳转系统设置页面
- Intent实现Android间的页面跳转
- Android开发关于Activity跳转另一个Activity指定的fragment的解决方法
- android如何从一个应用跳转到另一个页面
- Android开发,关于Intent跳转和传递值
- 关于Fragment之间页面跳转时的extra信息的传递
- 扫描二维码自动识别手机系统(Android/IOS)跳转不同页面
- 关于使用struts2时子窗体页面跳转后在父窗体打开的问题以及Session过期后的页面跳转问题
- 关于vb.net的web页面中页面跳转的问题
- android应用程序跳转到系统的各个设置页面
- Android Studio第三十八期 - HIOS跳转协议解决URI跳转原生页面并传递参数
- 关于form表单提交,如何跳转到一个带参数的页面
- Java-Android 之页面的跳转和结构的搭建