Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例
應(yīng)朋友們反饋的Android基礎(chǔ)薄弱的問題,決定出一套Android基礎(chǔ)教程,幫助大家復(fù)習(xí),鞏固Android基礎(chǔ),今天要講的是Android中的Intent實(shí)現(xiàn)Android間的頁面跳轉(zhuǎn)。
增加Acrivity頁面時(shí),首先需要在MainActivity中對頁面注冊,比如

新建被跳轉(zhuǎn)的頁面OtherActivity,其對應(yīng)的xml文件如下
activity_other
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:text="第二個(gè)Activity"/>
</LinearLayout>
Java代碼
OtherActivity
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class OtherActivity extends AppCompatActivity {
@Override
public void setContentView(View view) {
super.setContentView(R.layout.activity_other);
}
}
程序主界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個(gè)Activity"/>
<Button
android:id="@+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="頁面跳轉(zhuǎn)"/>
</LinearLayout>
Java代碼
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.start_btn);
startButton.setOnClickListener(new ButtonListener());
}
class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//當(dāng)點(diǎn)擊事件觸發(fā)后執(zhí)行,啟動(dòng)OtherActivity
//創(chuàng)建一個(gè)Intent對象
Intent intent =new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);//從MainActivity跳轉(zhuǎn)到OtherActivity
startActivity(intent);
}
}
}
另外除了上述的顯式Intent,還有隱式Intent,隱式Intent可以用來傳遞數(shù)組及動(dòng)作狀態(tài)
比如在MainActivity中
//當(dāng)點(diǎn)擊事件觸發(fā)后執(zhí)行,啟動(dòng)OtherActivity
//創(chuàng)建一個(gè)Intent對象
Intent intent =new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);//從MainActivity跳轉(zhuǎn)到OtherActivity
intent.putExtra("姓名","小李");
startActivity(intent);
在被跳轉(zhuǎn)的OtherActivity中
Intent intent =new Intent();
String name = intent.getStringExtra("姓名");
可以接收由MainActivity傳來的數(shù)據(jù)
又或者
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
可以調(diào)用撥打電話界面并設(shè)定預(yù)設(shè)號(hào)碼為10086
還可以設(shè)置網(wǎng)址的跳轉(zhuǎn),顯示地理位置等
如設(shè)置為跳轉(zhuǎn)打開網(wǎng)址時(shí),需要在AndroidManifast中注冊一下<data android:scheme="http"/>
如下:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)注冊頁面跳轉(zhuǎn)登錄頁面的創(chuàng)建
- Android實(shí)現(xiàn)頁面跳轉(zhuǎn)的全過程記錄
- Android使用Intent顯示實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android 實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android使用Circular Reveal動(dòng)畫讓頁面跳轉(zhuǎn)更炫酷
- Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能詳解
- Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
- Android實(shí)現(xiàn)頁面跳轉(zhuǎn)
相關(guān)文章
Android客制化adb shell進(jìn)去后顯示shell@xxx的標(biāo)識(shí)
今天小編就為大家分享一篇關(guān)于Android客制化adb shell進(jìn)去后顯示shell@xxx的標(biāo)識(shí),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android組件ContextMenu實(shí)現(xiàn)長按事件
這篇文章主要為大家詳細(xì)介紹了Android組件ContextMenu實(shí)現(xiàn)長按事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android 簡單實(shí)現(xiàn)倒計(jì)時(shí)功能
在 Android 中倒計(jì)時(shí)功能是比較常用的一個(gè)功能,比如短信驗(yàn)證碼,付款倒計(jì)時(shí)等。今天小編給大家分享Android 簡單實(shí)現(xiàn)倒計(jì)時(shí)功能,感興趣的朋友一起看看吧2019-12-12
讓Android中RadioGroup不顯示在輸入法上面的辦法
在Android開發(fā)中,發(fā)現(xiàn)一個(gè)問題,打開輸入法導(dǎo)致下面的radioGroup的位置發(fā)生了變化,被頂?shù)搅溯斎敕ǖ纳厦?,那么該如何解決呢?下面來看看。2016-08-08
VideoView實(shí)現(xiàn)視頻無縫連續(xù)播放
這篇文章主要為大家詳細(xì)介紹了VideoView實(shí)現(xiàn)視頻無縫連續(xù)播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android?startActivityForResult的調(diào)用與封裝詳解
startActivityForResult?可以說是我們常用的一種操作了,目前有哪些方式實(shí)現(xiàn)?startActivityForResult?的功能呢?本文就來和大家詳細(xì)聊聊2023-03-03
詳解Android中的MVP架構(gòu)分解和實(shí)現(xiàn)
本篇文章主要介紹了詳解Android中的MVP架構(gòu)分解和實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
Android 中CheckBox的isChecked的使用實(shí)例詳解
這篇文章主要介紹了Android 中CheckBox的isChecked的使用實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

