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

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

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

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

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

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

Intent實(shí)現(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);    /*     * 通過點(diǎn)擊bt1實(shí)現(xiàn)頁面之間的跳轉(zhuǎn)     * 1.startActivity來實(shí)現(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);    //注冊(cè)點(diǎn)擊事件    bt1.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        /**         * 第一個(gè)參數(shù),上下文對(duì)象this         * 第二個(gè)參數(shù),目標(biāo)文件         */        Intent intent = new Intent(mContext, SActivity.class);        startActivity(intent);      }    });    /*     * 通過startActivityForResult     * 第二個(gè)參數(shù)是請(qǐng)求的一個(gè)標(biāo)識(shí)     */    bt2.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        Intent intent = new Intent(mContext, SActivity.class);        startActivityForResult(intent, 1);      }    });  }  /*   * 通過startActivityForResult 跳轉(zhuǎn),接受返回?cái)?shù)據(jù)的方法   * requestCode:請(qǐng)求標(biāo)識(shí)   * resultCode:第二個(gè)頁面返回的標(biāo)識(shí)   * data 第二個(gè)頁面回傳的數(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="第一種啟動(dòng)方式" />  <Button    android:id="@+id/button2_second"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="第二種啟動(dòng)方式" />  <TextView    android:id="@+id/textView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="把第二個(gè)頁面回傳的數(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);    /*     * 第二個(gè)頁面什么時(shí)候回傳數(shù)據(jù)給第一個(gè)頁面     * 回傳到第一個(gè)頁面的,實(shí)際上是一個(gè)Intent對(duì)象     */    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" >      <!-- 首啟動(dòng)項(xiàng) -->      <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);

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 峨边| 仪征市| 拉孜县| 泌阳县| 盐山县| 嘉黎县| 茶陵县| 福鼎市| 江孜县| 湄潭县| 江城| 曲周县| 大田县| 德安县| 宁波市| 商水县| 綦江县| 沾益县| 惠来县| 丰顺县| 栾川县| 二连浩特市| 信丰县| 和田县| 柳河县| 余江县| 台前县| 会昌县| 绍兴县| 双鸭山市| 凉山| 昭通市| 随州市| 商丘市| 建德市| 吉隆县| 连山| 许昌县| 垫江县| 大同市| 烟台市|