日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android深入浅出系列之实例应用—手机页面之间的跳转

發布時間:2025/4/5 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android深入浅出系列之实例应用—手机页面之间的跳转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

?

  運行效果如下

????? 當我們單擊第一布局文件當中的按鈕后,就會切換到第二個我們設計好的布局文件。

  

????

總結

以上是生活随笔為你收集整理的Android深入浅出系列之实例应用—手机页面之间的跳转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。