Android Studio 之 通過 Intent 完成點擊按鈕實現頁面跳轉


 

•Intent 簡介

  Intent 是 Android 程序中各組件之間進行交互的一種重要方式;

  它不僅可以指明當前組件想要執行的動作,還可以在不同組件之間傳遞數據。

  Intent 有多個構造函數,其中一個是 Intent(Context packageContext, Class<?> cls):

    • 第一個參數 Context 要求提供一個啟動活動的上下文
    • 第二個參數 Class 則是指定想要啟動的目標活動

  通過這個構造函數可以構建出 Intent 的 “意圖”;

•設置跳轉界面

  新建一個 Empty Activity,命名為 AnotherActivity;

  修改 activity_another.xml 中的代碼;

activity_another.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="This is Another Activity!"
        android:textSize="20sp"
        android:textColor="@color/black"/>

</RelativeLayout>

  該代碼只放置了一個用於顯示文本的 TextView;

 

•跳轉前的准備工作

  在 activity_main.xml 中創建一個 Button 控件,並設置其 id 為 btn,代碼如下;

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="跳轉"
        />

</RelativeLayout><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="跳轉"
        />

</RelativeLayout>

•使用 Intent 完成跳轉

  接下來修改 MainActivity.java 中的代碼;

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button mBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtn = findViewById(R.id.btn);
        mBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
                startActivity(intent);
            }
        });
    }
}

  在 MainActivity.java 中創建一個 Button 變量 mBtn;

  通過 findViewById(R.id.btn) 找到 btn 這個組件;

  創建一個新的 Activity,姑且命名為 nextActivity;

  然后,通過為 mBtn 設置點擊事件完成界面跳轉;

mBtn.setOnClickListener(new View.OnClickListener(){

    public void onClick(View view){
        Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
        startActivity(intent);
    }

});

  通過  Intent intent=new Intent(); ,我們構造出了一個 Intent;

  傳入 MainActivity.this 作為上下文,傳入 AnotherActivity.class 作為活動目標;

  這樣我們的意圖就非常明顯了:在 MainActivity 這個活動的基礎上打開 AnotherActivity 這個活動;

  然后通過 startActivity() 方法來執行這個 Intent。

•運行效果

  

•其他跳轉方式

  除了使用  Intent intent=new Intent(MainActivity.this,AnotherActivity.class); 完成跳轉外,

  我們還可以這么寫:

        mBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AnotherActivity.class);
                startActivity(intent);
            }
        });

  通過  intent.setClass() 方法完成跳轉,效果是一樣的;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM