如何在Android中使用Intent實(shí)現(xiàn)一個(gè)頁面跳轉(zhuǎn)功能-創(chuàng)新互聯(lián)

如何在Android中使用Intent實(shí)現(xiàn)一個(gè)頁面跳轉(zhuǎn)功能?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)自2013年起,先為沈陽等服務(wù)建站,沈陽等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為沈陽企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

首先需要在MainActivity中對(duì)頁面注冊(cè),比如

如何在Android中使用Intent實(shí)現(xiàn)一個(gè)頁面跳轉(zhuǎn)功能

新建被跳轉(zhuǎn)的頁面OtherActivity,其對(duì)應(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對(duì)象 
      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對(duì)象 
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中注冊(cè)一下<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)容,你們掌握如何在Android中使用Intent實(shí)現(xiàn)一個(gè)頁面跳轉(zhuǎn)功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站標(biāo)題:如何在Android中使用Intent實(shí)現(xiàn)一個(gè)頁面跳轉(zhuǎn)功能-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.muchs.cn/article12/cdjpgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、移動(dòng)網(wǎng)站建設(shè)、微信公眾號(hào)、網(wǎng)站制作定制網(wǎng)站、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)