手机页面之间的跳转

在网页里,我们可以通过超级链接从一个网页跳转到另外一个网页,在手机里面,要如何实现手机页面之间的跳转呢?
  原理:通过布局文件和setContentView()方法配合来实现。通过点击第一个布局文件main.xml当中的按钮,加载第二个布局文件main2.xml,然后点击第二个布局文件main2.xml当中的按钮,加载第一个布局文件main.xml。

  1.1:第一个布局文件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"
  />
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="跳到第二个手机页面"
    android:id="@+id/btn1"
  />
  </LinearLayout>

  1.2:第二个布局文件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/hello2"
  />
<Button
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="跳到第一个手机页面"
  android:id="@+id/btn2"
  />
  </LinearLayout>
  1.3:字符文件stings.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
<string name="hello">我是第一个手机布局页面</string>
<string name="hello2">我是第二个手机布局页面</string>
<string name="app_name">setContentViewDemo</string>
  </resources>
  1.4:代码文件

  package com.menglin.setcontentview;

  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;

  public class MainActivity extends Activity
  {
    private Button btn1 = null;
    private Button btn2 = null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      //默认加载第一个布局文件
      setContentView(R.layout.main);
      //通过findViewById()方法得到第一个布局文件中的Button对象
      btn1 = (Button) findViewById(R.id.btn1);
      //给这个Button对象绑定监听器
      btn1.setOnClickListener(new Button1Listener());
    }

    //第一个布局文件中的按钮的监听器
    private class Button1Listener implements OnClickListener
   {
      @Override
      public void onClick(View v)
      {
        //加载第二个布局文件
        setContentView(R.layout.main2);
        //通过findViewById()方法得到第二个布局文件中的Button对象
        btn2 = (Button) findViewById(R.id.btn2);
        //给这个Button对象绑定监听器
        btn2.setOnClickListener(new Button2Listener());
      }
    }

    //第二个布局文件中的按钮的监听器
    private class Button2Listener implements OnClickListener
   {
      @Override
      public void onClick(View v)
      {
        //加载第一个布局文件
        setContentView(R.layout.main);
        //通过findViewById()方法得到第一个布局文件中的Button对象
        btn1 = (Button) findViewById(R.id.btn1);
        //给这个Button对象绑定监听器
        btn1.setOnClickListener(new Button1Listener());
      }
    }
  }

  注意:虽然是实现了界面的来回跳转,但是始终是同一个Activity,所以类变量,函数等都是公用的。

  运行效果如下

当我们单击第一布局文件当中的按钮后,就会切换到第二个我们设计好的布局文件。

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值