本文實(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ì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注