Android Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的兩種方法
本文實(shí)例為大家分享了Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的兩種的方法,供大家參考,具體內(nèi)容如下

下圖中兩個(gè)不同的方法就是兩種頁(yè)面之間跳轉(zhuǎn)的情況
1).跳轉(zhuǎn)不返回?cái)?shù)據(jù)
2).跳轉(zhuǎn)返回?cái)?shù)據(jù)

實(shí)例:
第一種啟動(dòng)方式(跳轉(zhuǎn)不返回?cái)?shù)據(jù))
第二種啟動(dòng)方式(跳轉(zhuǎn)返回?cái)?shù)據(jù))
先看第一種:


點(diǎn)擊第一種啟動(dòng)方式按鈕會(huì)出現(xiàn)右邊的圖,然后再點(diǎn)擊Button按鈕返回左邊的界面,TextView中的內(nèi)容沒(méi)變。
再看第二種啟動(dòng)方式
不同的是,點(diǎn)擊Button按鈕返回左邊的界面,TextView中的內(nèi)容變成了你好。



下面是所有代碼
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.intent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name="com.example.lenovo.intent.firstactivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.lenovo.intent.Secondactivity">
</activity>
</application>
</manifest>
factivity
<?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/bt1__first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一種啟動(dòng)方式" />
<Button
android:id="@+id/bt2__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è)頁(yè)面回傳的數(shù)據(jù)顯示出來(lái)" />
</LinearLayout>
sactivity
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
firstactivity.java
package com.example.lenovo.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by lenovo on 2018/2/27.
*/
public class firstactivity extends Activity {
private Button bt1;
private Button bt2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
/*
通過(guò)點(diǎn)擊bt1實(shí)現(xiàn)界面之間的跳轉(zhuǎn)
1.通過(guò)startActivity的方式來(lái)實(shí)現(xiàn)
1>初始Intent(意圖)
*/
bt1=(Button) findViewById(R.id.bt1__first);
bt2=(Button)findViewById(R.id.bt2__second);
tv=(TextView) findViewById(R.id.textView1);
//給bt1添加點(diǎn)擊事件
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
第一個(gè)參數(shù):上下文對(duì)象this
第二個(gè)參數(shù):目標(biāo)文件
*/
Intent intent = new Intent(firstactivity.this,Secondactivity.class);
startActivity(intent);
}
});
/*
2.通過(guò)startActivityForResult的方式來(lái)實(shí)現(xiàn)
*/
//給bt2添加點(diǎn)擊事件
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(firstactivity.this,Secondactivity.class);
/*
第一個(gè)參數(shù):Intent對(duì)象
第二個(gè)參數(shù):請(qǐng)求的一個(gè)標(biāo)識(shí)
*/
startActivityForResult(intent,1);
}
});
}
/*
通過(guò)startActivityForResult的方式接受返回?cái)?shù)據(jù)的方法
requestCode:請(qǐng)求的標(biāo)志,給每個(gè)頁(yè)面發(fā)出請(qǐng)求的標(biāo)志不一樣,這樣以后通過(guò)這個(gè)標(biāo)志接受不同的數(shù)據(jù)
resultCode:這個(gè)參數(shù)是setResult(int resultCode,Intent data)方法傳來(lái)的,這個(gè)方法用在傳來(lái)數(shù)據(jù)的那個(gè)頁(yè)面
*/
@Override
protected void onActivityResult(int requestCode,int resultCode ,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==1&&resultCode==2){//當(dāng)請(qǐng)求碼是1&&返回碼是2進(jìn)行下面操作
String content=data.getStringExtra("data");
tv.setText(content);
}
}
}
Secondactivity.java
package com.example.lenovo.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by lenovo on 2018/2/27.
*/
public class Secondactivity extends Activity {
private Button bt;
String content="你好";//想返回的內(nèi)容
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
/*
第二個(gè)頁(yè)面什么時(shí)候給第一個(gè)頁(yè)面回傳數(shù)據(jù)
回傳到第一個(gè)頁(yè)面的實(shí)際上是一個(gè)Intent對(duì)象
*/
bt=(Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent();
//name相當(dāng)于一個(gè)key,content是返回的內(nèi)容
data.putExtra("data",content);
//resultCode是返回碼,用來(lái)確定是哪個(gè)頁(yè)面?zhèn)鱽?lái)的數(shù)據(jù),這里設(shè)置返回碼是2
//這個(gè)頁(yè)面?zhèn)鱽?lái)數(shù)據(jù),要用到下面這個(gè)方法setResult(int resultCode,Intent data)
setResult(2,data);
//結(jié)束當(dāng)前頁(yè)面
finish();
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android顯式Intent與隱式Intent的使用詳解
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問(wèn)題解決
- Android開(kāi)發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
- Android13?加強(qiáng)Intent?filters?的安全性
- android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android Intent傳遞數(shù)據(jù)大小限制詳解
- Android開(kāi)發(fā)中Intent.Action各種常見(jiàn)的作用匯總
- Android使用Intent隱式實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android Intent基礎(chǔ)用法及作用詳解
相關(guān)文章
Android應(yīng)用程序窗口(Activity)窗口對(duì)象(Window)創(chuàng)建指南
本文將詳細(xì)介紹Android應(yīng)用程序窗口(Activity)的窗口對(duì)象(Window)的創(chuàng)建過(guò)程,需要了解的朋友可以參考下2012-12-12
Android 中LayoutInflater.inflate()方法的介紹
這篇文章主要介紹了Android 中LayoutInflater.inflate()方法的介紹的相關(guān)資料,希望通過(guò)本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Android使用動(dòng)畫(huà)設(shè)置ProgressBar進(jìn)度的方法
這篇文章主要為大家詳細(xì)介紹了Android使用動(dòng)畫(huà)設(shè)置ProgressBar進(jìn)度的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android?Flutter實(shí)現(xiàn)任意拖動(dòng)的控件
使用flutter開(kāi)發(fā)是需要控件能拖動(dòng),比如畫(huà)板中的元素,或者工具條等,所以本文為大家準(zhǔn)備了Flutter實(shí)現(xiàn)任意拖動(dòng)控件的示例代碼,希望對(duì)大家有所幫助2023-07-07
完美解決EditText和ScrollView的滾動(dòng)沖突(上)
這篇文章主要為大家詳細(xì)介紹了完美解決EditText和ScrollView滾動(dòng)沖突的方法,感興趣的小伙伴們可以參考一下2016-06-06

