亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android編程中Intent實現(xiàn)頁面跳轉(zhuǎn)功能詳解

 更新時間:2017年07月28日 11:12:25   作者:LoveJulin  
這篇文章主要介紹了Android編程中Intent實現(xiàn)頁面跳轉(zhuǎn)功能,結(jié)合實例形式分析了Android Intent實現(xiàn)頁面跳轉(zhuǎn)功能的具體步驟與相關(guān)注意事項,需要的朋友可以參考下

本文實例講述了Android編程中Intent實現(xiàn)頁面跳轉(zhuǎn)功能。分享給大家供大家參考,具體如下:

安卓四大組件:Activity、Service、Broadcast Receiver、Content Provider

Intent實現(xiàn)頁面之間跳轉(zhuǎn)

1、無返回值

startActivity(intent)

2、有返回值

startActivityForResult(intent,requestCode);
onActivityResult(int requestCode,int resultCode,Intent data)
setResult(resultCode,data);

FActivity.java

package com.example.hello;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FActivity extends Activity{
  private Button bt1;
  private Context mContext;
  private Button bt2;
  private TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.factivity);
    /*
     * 通過點擊bt1實現(xiàn)頁面之間的跳轉(zhuǎn)
     * 1.startActivity來實現(xiàn)跳轉(zhuǎn)
     * 1>初始換Intent
     */
    mContext = this;
    bt1 = (Button) findViewById(R.id.button1_first);
    bt2 = (Button) findViewById(R.id.button2_second);
    tv = (TextView) findViewById(R.id.textView1);
    //注冊點擊事件
    bt1.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        /**
         * 第一個參數(shù),上下文對象this
         * 第二個參數(shù),目標(biāo)文件
         */
        Intent intent = new Intent(mContext, SActivity.class);
        startActivity(intent);
      }
    });
    /*
     * 通過startActivityForResult
     * 第二個參數(shù)是請求的一個標(biāo)識
     */
    bt2.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(mContext, SActivity.class);
        startActivityForResult(intent, 1);
      }
    });
  }
  /*
   * 通過startActivityForResult 跳轉(zhuǎn),接受返回數(shù)據(jù)的方法
   * requestCode:請求標(biāo)識
   * resultCode:第二個頁面返回的標(biāo)識
   * data 第二個頁面回傳的數(shù)據(jù)
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == 2) {
      String content = data.getStringExtra("data");
      tv.setText(content);
    }
  }
}

factivity.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" >
  <Button
    android:id="@+id/button1_first"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="第一種啟動方式" />
  <Button
    android:id="@+id/button2_second"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="第二種啟動方式" />
  <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="把第二個頁面回傳的數(shù)據(jù)顯示出來" />
</LinearLayout>

SActivity.java

package com.example.hello;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SActivity extends Activity{
  private Button bt;
  private String content = "你好";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sactivity);
    /*
     * 第二個頁面什么時候回傳數(shù)據(jù)給第一個頁面
     * 回傳到第一個頁面的,實際上是一個Intent對象
     */
    bt = (Button) findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent data = new Intent();
        data.putExtra("data", content);
        setResult(2, data);
        //結(jié)束當(dāng)前頁面
        finish();
      }
    });
  }
}

sactivity.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" >
  <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.hello"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name" >
    </activity>
    <activity
      android:name=".FActivity"
      android:label="@string/app_name" >
      <!-- 首啟動項 -->
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity
      android:name=".SActivity"
      android:label="@string/app_name" >
    </activity>
  </application>
</manifest>

用瀏覽器打開網(wǎng)頁

Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android自定義軟鍵盤的步驟記錄

    Android自定義軟鍵盤的步驟記錄

    Android軟鍵盤這塊從我入職到現(xiàn)在,是一個一直糾纏我的問題,這篇文章主要給大家介紹了關(guān)于Android自定義軟鍵盤的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Android Intent基礎(chǔ)用法及作用詳解

    Android Intent基礎(chǔ)用法及作用詳解

    Intent是一種重要的消息傳遞對象,用于在不同組件(如活動(Activity)、服務(wù)(Service)、廣播接收器(BroadcastReceiver)等)之間進(jìn)行通信和交互,本文介紹Android Intent基礎(chǔ)用法及作用,感興趣的朋友一起看看吧
    2024-07-07
  • Android使用DrawerLayout仿QQ6.0雙側(cè)滑菜單

    Android使用DrawerLayout仿QQ6.0雙側(cè)滑菜單

    這篇文章主要為大家詳細(xì)介紹了Android使用DrawerLayout仿QQ6.0雙側(cè)滑菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android實現(xiàn)通訊錄功能

    Android實現(xiàn)通訊錄功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)通訊錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Android自定義ViewGroup實現(xiàn)淘寶商品詳情頁

    Android自定義ViewGroup實現(xiàn)淘寶商品詳情頁

    這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實現(xiàn)淘寶商品詳情頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法

    Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法

    這篇文章主要介紹了Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法,實例分析了Android操作SQLite數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • Android NDK開發(fā)(C語言--動態(tài)內(nèi)存分配)

    Android NDK開發(fā)(C語言--動態(tài)內(nèi)存分配)

    這篇文章主要介紹了Android NDK開發(fā) C語言--動態(tài)內(nèi)存分配
    2021-12-12
  • 最新評論