Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能詳解
本文實(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);
//注冊點(diǎn)擊事件
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/**
* 第一個(gè)參數(shù),上下文對象this
* 第二個(gè)參數(shù),目標(biāo)文件
*/
Intent intent = new Intent(mContext, SActivity.class);
startActivity(intent);
}
});
/*
* 通過startActivityForResult
* 第二個(gè)參數(shù)是請求的一個(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:請求標(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對象
*/
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);
更多關(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è)計(jì)有所幫助。
相關(guān)文章
Android編程開發(fā)之Spinner控件用法實(shí)例分析
這篇文章主要介紹了Android編程開發(fā)之Spinner控件用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了下拉列表Spinner的具體使用技巧,需要的朋友可以參考下2015-12-12
Android學(xué)習(xí)之AppWidget筆記分享
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)筆記之AppWidget的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08
Android使用DrawerLayout仿QQ6.0雙側(cè)滑菜單
Android自定義ViewGroup實(shí)現(xiàn)淘寶商品詳情頁
Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
Android NDK開發(fā)(C語言--動(dòng)態(tài)內(nèi)存分配)

