国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Intent實現(xiàn)頁面跳轉(zhuǎn)的方法示例

2019-10-22 18:13:57
字體:
供稿:網(wǎng)友

應朋友們反饋的Android基礎(chǔ)薄弱的問題,決定出一套Android基礎(chǔ)教程,幫助大家復習,鞏固Android基礎(chǔ),今天要講的是Android中的Intent實現(xiàn)Android間的頁面跳轉(zhuǎn)

增加Acrivity頁面時,首先需要在MainActivity中對頁面注冊,比如

Android,Intent,頁面跳轉(zhuǎn)

新建被跳轉(zhuǎn)的頁面OtherActivity,其對應的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="第二個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="第一個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) {       //當點擊事件觸發(fā)后執(zhí)行,啟動OtherActivity       //創(chuàng)建一個Intent對象       Intent intent =new Intent();       intent.setClass(MainActivity.this,OtherActivity.class);//從MainActivity跳轉(zhuǎn)到OtherActivity       startActivity(intent);     }   } } 

另外除了上述的顯式Intent,還有隱式Intent,隱式Intent可以用來傳遞數(shù)組及動作狀態(tài)

比如在MainActivity中

//當點擊事件觸發(fā)后執(zhí)行,啟動OtherActivity //創(chuàng)建一個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è)定預設(shè)號碼為10086

還可以設(shè)置網(wǎng)址的跳轉(zhuǎn),顯示地理位置等

如設(shè)置為跳轉(zhuǎn)打開網(wǎng)址時,需要在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)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 克拉玛依市| 绩溪县| 任丘市| 巴林左旗| 玛纳斯县| 昂仁县| 罗平县| 余庆县| 福安市| 吉安市| 永定县| 朝阳市| 竹溪县| 木里| 朔州市| 凌海市| 东阳市| 濮阳市| 天全县| 江口县| 文化| 绩溪县| 贡山| 枞阳县| 枣强县| 桑日县| 田阳县| 科技| 沧源| 肃北| 南涧| 乐业县| 台山市| 乐昌市| 南部县| 柳江县| 临沂市| 临沧市| 巴彦淖尔市| 九龙城区| 招远市|