Android深入淺出系列之執行個體應用—手機頁面之間的跳轉

來源:互聯網
上載者:User

  在網頁裡,我們可以通過超級連結從一個網頁跳轉到另外一個網頁,在手機裡面,要如何?手機頁面之間的跳轉呢?
  原理:通過布局檔案和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,所以類變數,函數等都是公用的。

 

  運行效果如下

      當我們單擊第一布局檔案當中的按鈕後,就會切換到第二個我們設計好的布局檔案。

  

    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.