Activity跳轉(zhuǎn)和傳值
1.重點內(nèi)容
1.重點內(nèi)容
- Activity之間的跳轉(zhuǎn)及數(shù)據(jù)傳遞(如何跳轉(zhuǎn)?如何返回?)
- 使用Intent在Activity之間傳遞數(shù)據(jù))(單向和雙向?)
- 回傳數(shù)據(jù)(怎么回傳和接收?)
- 隱式啟動Activity(代碼量少,可以實現(xiàn)不同組件之間的跳轉(zhuǎn))
2.使用Intent顯式啟動Activity
2.1Intent簡介
1.引入
比如下面的圖,當(dāng)我點擊”Go to Activity2“的時候就會從第一個Activity跳轉(zhuǎn)到下一個Activity。再點擊”Go to Activity1“就會跳轉(zhuǎn)到第一個Aactivity。
2.多個Activity之間的關(guān)系
在android開發(fā)中,經(jīng)常會從一個頁面調(diào)轉(zhuǎn)到另一個頁面,在android中從一個activity啟動另一個activity可以使用startActivity方法或者startActivityForResult方法(能返回值)。這里的兩個方法要傳遞參數(shù)時需要使用到對象Intent,intent是信使,完成組件之間的通信功能。
3.Intent簡介
白話:Intent起到了不同Activity之間組件傳值的作用。就是傳值。傳數(shù)據(jù)。
簡介:
- Intent一般用于啟動Activity,啟動服務(wù),發(fā)送廣播等,承擔(dān)了Android應(yīng)用程序核心組件相互間的通信功能。
- Intent是一種運行時綁定(runtime binding)機制,它能在程序運行的過程中連接兩個不同的組件。通過Intent,你的程序可以向Android表達某種請求或者意愿,Android會根據(jù)意愿的內(nèi)容選擇適當(dāng)?shù)慕M件來響應(yīng)。比如,有一個Activity希望打開網(wǎng)頁瀏覽器查看某一網(wǎng)頁的內(nèi)容,那么這個Activity只需要發(fā)出WEBSEARCHACTION請求給Android,Android 會根據(jù)Intent的內(nèi)容,查詢各組件注冊時的聲明的IntentFilter,找到網(wǎng)頁瀏覽器Activity來瀏覽網(wǎng)頁。
4.Intent簡介圖示:
可參考文檔:https://www.cnblogs.com/mengdd/archive/2013/03/18/2965839.html
- Intent是Activity、Service、Broadcast Receiver之間的橋梁,為Activity、Service和BroadcastReceiver等組件提供交互能力
- Intent對象必須需要包含一個信息包,用于接收和傳遞組件需要的信息
5.Intent的兩種調(diào)用方式?
顯示調(diào)用需要明確的指定需要啟動的Activity所在位置,隱式調(diào)用不需要指定組件的名稱,但需要一定的條件進行過濾。
- 顯式調(diào)用,必須在Intent中Component name指明啟動的Activity所在的類;
- 隱式調(diào)用,沒有明確指定目標組件的名稱,那么就要通過一定的條件過濾篩選。
- Android系統(tǒng)根據(jù)隱式意圖中設(shè)置的動作(Action)、類別(category)、數(shù)據(jù)(Uri和數(shù)據(jù)類型)決定啟動哪一個組件。
也就是說在隱式啟動時,Intent中只包含需要執(zhí)行的動作和所包含的數(shù)據(jù),而無需指明具體啟動哪一個Activity,選擇權(quán)由Android系統(tǒng)和最終用戶來決定。
2.2Intent的顯式調(diào)用
1.顯式調(diào)用步驟?
- 創(chuàng)建一個Intent對象(new Intent())
- 指定當(dāng)前的應(yīng)用程序上下文以及要啟動的Activity(setClass())
- 把創(chuàng)建好的這個Intent作為參數(shù)傳遞給startActivity()方法(調(diào)用startActivity())
2.2.1采用Intent有參構(gòu)造函數(shù)
1.語法:
第一個參數(shù)傳入的就是this,第二個需要指定是目標組件的類
2.案例1:
主布局文件的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊按鈕跳轉(zhuǎn)到不同的activity"
/>
<Button
android:id="@+id/helloActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helloActivity"
/>
</LinearLayout>
主布局文件的java文件代碼:
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//顯式的調(diào)用activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// helloActivity
Button hello=findViewById(R.id.helloActivity);
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 傳遞上下文和目標組件
Intent intent=new Intent(MainActivity.this,HelloActivity.class);
// 啟動Intent
startActivity(intent);
}
});
}
}
要跳轉(zhuǎn)的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HelloActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是HelloActivity"
android:textSize="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
效果圖如下:
2.2.2采用Intent無參構(gòu)造函數(shù)構(gòu)造一個空Intent對象
1.創(chuàng)建的步驟
2.案例2:
- 和案例1的區(qū)別在圈出來的地方,多調(diào)用setClass方法把上下文對象和對應(yīng)的類傳入進去就可以。
擴展:關(guān)于上下文對象的多種書寫的方式
2.3用Intent在Activity之間傳遞數(shù)據(jù)(單向)
2.3.1傳值的簡介
1.簡介:當(dāng)一個Activity啟動另一個Activity時,常常會有一些數(shù)據(jù)要傳遞過去,在Android中,需要傳遞的數(shù)據(jù)放在對象Intent中。
比如說:提交一個表單的時候,需要往下一個activity傳遞數(shù)據(jù)。
獲取前一個activity傳遞過來的數(shù)據(jù)。
所以本小節(jié),主要學(xué)習(xí)的是如何獲取前一個activity傳遞的數(shù)據(jù)。
2.常見的傳遞數(shù)據(jù)的方法:
2.3.2使用Intent+putExtra的單向簡單傳值
第一步:創(chuàng)建登錄的界面
第二步:編寫登錄界面的布局文件,代碼如下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity">
<TextView
android:textSize="30dp"
android:layout_gravity="center"
android:textColor="@color/purple_700"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="當(dāng)前頁面是登錄界面"
/>
<!--賬號和密碼的輸入框-->
<EditText
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:inputType="text"
android:hint="輸入賬號"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:inputType="textPassword"
android:hint="輸入密碼"
/>
<!-- 登錄按鈕-->
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
/>
</LinearLayout>
第三步:編寫登錄界面的activity,代碼如下。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
//登錄界面
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// 獲取控件
Button login=findViewById(R.id.login);
EditText account=findViewById(R.id.account);
EditText password=findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 獲取登錄的賬號和密碼
String acc= account.getText().toString();
String pswd=password.getText().toString();
// 創(chuàng)建Intent對象傳遞數(shù)據(jù)
Intent intent=new Intent();
intent.setClass(getBaseContext(),LoginHandleActivity.class);
// 設(shè)置需要傳遞的數(shù)據(jù)
intent.putExtra("account",acc);
intent.putExtra("password",pswd);
startActivity(intent);
}
});
}
}
第三步:創(chuàng)建接收數(shù)據(jù)的activity和布局文件。
第四步:顯示數(shù)據(jù)的布局文件代碼如下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginHandleActivity">
<TextView
android:id="@+id/loginsuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30dp"
/>
</LinearLayout>
第五步:接收數(shù)據(jù)的activity代碼如下。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class LoginHandleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_handle);
// 登錄成功的提示
TextView loginsuccess=findViewById(R.id.loginsuccess);
// 獲取傳遞的數(shù)值
Intent intent=getIntent();
String account=intent.getStringExtra("account");
String password=intent.getStringExtra("password");
// 設(shè)置提示內(nèi)容
loginsuccess.setText("恭喜 用戶:"+account+" 密碼為:"+password+" 登錄成功!");
// 打印提示消息
Toast.makeText(this, ""+loginsuccess.getText(), Toast.LENGTH_SHORT).show();
}
}
效果圖:文章來源:http://www.zghlxwxcb.cn/news/detail-409101.html
2.3.3使用Intent+Bundle的復(fù)雜傳值
1.簡介:使用Bundle實現(xiàn)數(shù)據(jù)傳遞。Bundle是數(shù)據(jù)攜帶包,提供了多個方法存入數(shù)據(jù)。
需要先創(chuàng)建一個Bundle對象
往bundle中傳遞數(shù)據(jù)
- putXxx(String key,Xxx data):向Bundle中放入各種類型數(shù)據(jù)。
- putSerializable(String key,Serializable data):向Bundle中放入一個可序列化的對象。(常常是自己創(chuàng)建的對象)
為了取出Bundle數(shù)據(jù)攜帶包里的數(shù)據(jù),Bundle提供如下方法。
獲取Bundle中的數(shù)據(jù):
- getXxx(String key):從Bundle取出Int、Long等各數(shù)據(jù)類型數(shù)據(jù)。
- getSerializable(String key):從Bundle取出一個可序列化的對象。
2.案例3:-注冊界面
第一步:創(chuàng)建一個User,包含id和name以及age屬性。
package com.example.activity_study;
import java.io.Serializable;
//創(chuàng)建一個可序列化的對象
public class User implements Serializable {
private String id;
private String password;
private int age;
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
public User(String id, String password, int age) {
this.id = id;
this.password = password;
this.age = age;
}
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
第二步:創(chuàng)建注冊的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegisterActivity">
<!-- 創(chuàng)建的是一個注冊的界面-->
<TextView
android:textSize="30dp"
android:layout_gravity="center"
android:textColor="@color/purple_700"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="當(dāng)前頁面是登注冊界面"
/>
<!--賬號和密碼的輸入框-->
<EditText
android:id="@+id/regid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:inputType="text"
android:hint="輸入賬號"
/>
<EditText
android:id="@+id/regpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:inputType="textPassword"
android:hint="輸入密碼"
/>
<EditText
android:id="@+id/regage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:inputType="textPassword"
android:hint="輸入年齡"
/>
<!-- 注冊按鈕-->
<Button
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
/>
</LinearLayout>
第三步:編寫注冊的activity。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// 獲取控件
Button login=findViewById(R.id.reg);
EditText account=findViewById(R.id.regid);
EditText password=findViewById(R.id.regpassword);
EditText regage=findViewById(R.id.regage);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 獲取登錄的賬號和密碼
String acc= account.getText().toString();
String pswd=password.getText().toString();
int age=Integer.parseInt(regage.getText().toString());
// 創(chuàng)建可序列化對象
User user=new User(acc,pswd,age);
String desc="當(dāng)前傳入的對象是一個可序列化的對象";
// 創(chuàng)建Bundle
Bundle bundle=new Bundle();
bundle.putString("desc",desc);
bundle.putSerializable("user",user);
// 創(chuàng)建Intent對象傳遞數(shù)據(jù)
Intent intent=new Intent();
intent.setClass(getBaseContext(),RegisterHandleActivity.class);
// 設(shè)置需要傳遞的數(shù)據(jù)(直接傳遞bundle數(shù)據(jù)包就可以)
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
第四步:編寫接收信息的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".RegisterHandleActivity">
<TextView
android:id="@+id/regsuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30dp"
/>
</LinearLayout>
第五步:編寫接收信息的activity文件。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterHandleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_handle);
// 登錄成功的提示
TextView loginsuccess=findViewById(R.id.regsuccess);
// 獲取傳遞的數(shù)值
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String desc=bundle.getString("desc");
// 獲取可序列化的對象
User user=(User) bundle.getSerializable("user");
// 設(shè)置提示內(nèi)容
loginsuccess.setText(desc+" 恭喜 用戶"+user.toString());
// 打印提示消息
Toast.makeText(this, ""+loginsuccess.getText(), Toast.LENGTH_SHORT).show();
}
}
效果圖:
2.4用Intent在Activity之間傳遞數(shù)據(jù)(雙向)
1.簡介:
第一個頁面需要設(shè)置傳遞的數(shù)據(jù)+請求碼。第二個頁面需要設(shè)置結(jié)果碼。第一個頁面接收數(shù)據(jù)+請求碼+結(jié)果碼。
- Activity提供了一個startActivityForResult(Intent intent,int requestCode )方法來啟動其他Activity。該方法用于啟動指定的Activity,而且期望獲取指定Activity返回的結(jié)果。
- 為了獲取被啟動的Activity所返回的結(jié)果,當(dāng)前Activity需要重寫onActivityResult(int requestCode,int resultCode,Intent intent)。
- 一個Activity中可能包含多個按鈕,并調(diào)用startActivityForResult()方法來打開多個不同的Activity處理不同的業(yè)務(wù)。當(dāng)這些新Activity關(guān)閉時,系統(tǒng)都會調(diào)用前面Activity的onActivityResult(int requestCode,int resultCode,Intent intent)方法,利用request Code區(qū)分是哪個請求結(jié)果觸發(fā)的,利用resultCode區(qū)分返回的數(shù)據(jù)來自哪個新的Activity。
2.回傳數(shù)據(jù)的步驟
注意:注意:
在一個Activity中可能會調(diào)用startActivityForResult()方法啟動多個Activity,每一個Activity返回的數(shù)據(jù)都會回調(diào)到onActivityResult()這個方法中,所以需要通過檢查requestCode的值來判斷數(shù)據(jù)來源,確定數(shù)據(jù)是從正確的Activity返回,然后再通過resultCode的值來判斷數(shù)據(jù)處理結(jié)果是否成功,最后從data中取出數(shù)據(jù)并打印。
- Step1:第1個Activity中使用startActivityForResult()方法實現(xiàn)回傳數(shù)據(jù)。
- intent,int requestCode)
第一個參數(shù)是Intent,第二個參數(shù)是請求碼,用于判斷數(shù)據(jù)的來源
- Step2:第2個Activity中添加數(shù)據(jù)返回代碼。需要使用setResult()方法。
setResult(int resultCode,Intent data)
resultCode:結(jié)果碼,一般使用0或1;
Intent:帶有數(shù)據(jù)的Intent - Step3:第1個Activity中使用onActivityResult()方法獲取返回的數(shù)據(jù)。
protected void onActivityResult(int requestCode,int resultCode,Intent data)
requestCode:啟動Activity時傳遞的請求碼;
resultCode:表示在返回數(shù)據(jù)時傳入結(jié)果碼;
data:攜帶返回數(shù)據(jù)的Intent
2.4.1雙向傳遞數(shù)據(jù)(聊天)
第一步:創(chuàng)建A的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".personOne">
<!-- TextView顯示當(dāng)前的聊天的信息-->
<TextView
android:id="@+id/Artext"
android:textSize="30dp"
android:textColor="@color/purple_200"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第i=0輪,消息為空"
/>
<!-- 輸入需要傳遞的數(shù)據(jù)-->
<EditText
android:textSize="30dp"
android:id="@+id/Astext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入需要發(fā)送給B的信息"
/>
<Button
android:id="@+id/AtoB"
android:textSize="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我發(fā)送給B"
/>
</LinearLayout>
第二步:創(chuàng)建A的activity代碼
package com.example.activity_study;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//A用戶
public class personOne extends AppCompatActivity {
static int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person_one);
EditText Astext=findViewById(R.id.Astext);
Button AtoB=findViewById(R.id.AtoB);
// 接收數(shù)據(jù)
AtoB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 傳遞A發(fā)送給B的數(shù)據(jù)
String textA=Astext.getText().toString();
Astext.setText("");
Intent intent=new Intent();
intent.setClass(getBaseContext(),personTwo.class);
intent.putExtra("msg",textA);
startActivityForResult(intent,0);
}
});
}
// 接收數(shù)據(jù)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println(requestCode+"=======");
if (requestCode==0&&resultCode==1){
// 獲取控件
TextView Artext=findViewById(R.id.Artext);
if (!data.getStringExtra("msg").toString().equals(""))
{
i++;
Artext.setText(data.getStringExtra("第i="+i+"輪,"+"msg"));
}
else{
Artext.setText("消息為空");
}
}
}
}
第三步:創(chuàng)建B的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".personTwo">
<!-- TextView顯示當(dāng)前的聊天的信息-->
<TextView
android:id="@+id/Brtext"
android:textSize="30dp"
android:textColor="@color/purple_200"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消息為空"
/>
<!-- 輸入需要傳遞的數(shù)據(jù)-->
<EditText
android:id="@+id/Bstext"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入需要發(fā)送給A的信息"
/>
<Button
android:id="@+id/BtoA"
android:textSize="30dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我發(fā)送給A"
/>
</LinearLayout>
第四步:創(chuàng)建B的activity代碼。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class personTwo extends AppCompatActivity {
static int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person_two2);
// B發(fā)送給A的數(shù)據(jù)
EditText Bstext=findViewById(R.id.Bstext);
// B接收A的消息
TextView Brtext=findViewById(R.id.Brtext);
// 傳遞數(shù)據(jù)的按鈕
Button BtoA=findViewById(R.id.BtoA);
// 接收數(shù)據(jù)
i++;
String msg=getIntent().getStringExtra("msg");
if (!msg.toString().equals("")){
Brtext.setText("第i="+i+"輪消息,"+msg);
}
else
{
Brtext.setText("第i="+i+"輪,消息為空");
}
BtoA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// B發(fā)送給A的數(shù)據(jù)
String textB=Bstext.getText().toString();
Bstext.setText("");
Intent intent=new Intent();
// 回傳數(shù)據(jù)不用設(shè)置class
intent.putExtra("msg",textB);
// 回傳數(shù)據(jù)
setResult(1,intent);
finish();
}
});
}
}
效果圖:
3.使用Intent隱式啟動Activity
3.1簡介
1.隱式傳遞的優(yōu)點:
- 優(yōu)點:不需要指明啟動哪一個Activity,而由Android系統(tǒng)來決定,有利于使用第三方組件。
- 隱式啟動Activity時,Android系統(tǒng)在應(yīng)用程序運行時解析Intent,并根據(jù)一定的規(guī)則對Intent和Activity進行匹配,使Intent上的動作、數(shù)據(jù)與Activity完全吻合。
- 匹配的Activity可以是應(yīng)用程序本身的,也可以是Android系統(tǒng)內(nèi)置的,還可以是第三方應(yīng)用程序提供的。因此,這種方式更加強調(diào)了Android應(yīng)用程序中組件的可復(fù)用性
3.2IntentFilter
1.簡介:
- IntentFilter中具有和Intent對應(yīng)的用于過濾動作Action,數(shù)據(jù)Data和類別Category的字段,根據(jù)這些字段對適合接收該Intent的組件進行匹配和篩選的機制。
- 每個 IntentFilter描述該組件所能響應(yīng)Intent請求的能力——組件希望接收什么類型的請求行為,什么類型的請求數(shù)據(jù)。
2.常見的屬性
- 為了使組件能夠注冊IntentFilter,通常在AndroidManifest.xml文件的各個組件的節(jié)點下定義節(jié)點,然后在節(jié)點聲明該組件所支持的動作、執(zhí)行的環(huán)境和數(shù)據(jù)格式等信息。當(dāng)然,也可以在程序代碼中動態(tài)地為組件設(shè)置IntentFilter。節(jié)點支持標簽、標簽和標簽,分別用來定義IntentFilter的“動作”、“類別”和“數(shù)據(jù)”。
Android平臺通知BroadcastReceiver對象時需要給該對象發(fā)送一個Intent對象,如果該Intent對象符合action,category,data等屬性值則啟動該BroadcastReceiver對象,如果不符合這個標準,即intent-filter中沒有這個屬性能和Intent中的屬性匹配,則不會啟動該BroadcastReceiver。
3.屬性列表
- Action屬性是一個字符串, 代表某一種特定的動作
Category屬性也是一個字符串, 用于指定一些目標組件需要滿足的額外條件,包含了處理該Intent的組件的種類信息, 起著對action的補充說明作用。 - Data屬性指定所操作數(shù)據(jù)。
- Android中的Intent通過Action,Category和data等屬性進行了相應(yīng)的描述,我們想做某些事情(達成某些目標),就需要填寫這些參數(shù)的部分或全部,這樣Android才會幫助我們自動的去進行某些操作。
Intent(發(fā)送數(shù)據(jù)的廣播對象)如何處理數(shù)據(jù)是由Action和data兩部分決定的,Action指的是處理數(shù)據(jù)的方法,即操作是什么類型的;data是要被處理的數(shù)據(jù)。如動作Action“洗”,對象data“衣服”,對于不同的數(shù)據(jù),處理的動作不同。如洗衣服和洗臉的步驟不同。
4.隱式啟動設(shè)置的步驟
注意事項:
如果自己定義的某個Activity要通過隱式啟動,在AndroidManifast.xm那么必須加上android.intent.category.DEFAULT,否則不起作用.
在activity中
Intent newIntent=new Intent();
newIntent.setAction(“www.yzh.cn”); newIntent.addCategory(“www”);//category不匹配,則無法啟動secondactivity。如果不加這條語句,也就是說不用匹配category,只要有程序也可以運行。但是如果要加addCategory(),則一定要準確。
- step1:在AndroidManifest.xml文件里
- step2:action 標簽指明了當(dāng)前Activity可以響應(yīng)的動作為“ cn.itscast.xxx”,而category標簽則包含了一些類別信息,只有當(dāng)、中的內(nèi)容同時匹配時,Activity才會被啟動。
- step3:隱式啟動的代碼如下:Activity中
3.2.1隱式跳轉(zhuǎn)的案例
第一步:創(chuàng)建主布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MorningActivity">
<!-- 隱式跳轉(zhuǎn)-->
<Button
android:id="@+id/btnmorning"
android:layout_gravity="center"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我隱式跳轉(zhuǎn)"
/>
</LinearLayout>
第二步:創(chuàng)建隱式啟動的activity。
- 和顯式啟動的不同點如下。
隱式啟動的代碼如下:
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//設(shè)置隱式啟動的activity
public class MorningActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_morning);
Button btnmorning=findViewById(R.id.btnmorning);
btnmorning.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 設(shè)置隱式跳轉(zhuǎn)
Intent intent=new Intent();
intent.setAction("www.morning.action");
// 設(shè)置Deault的就不用設(shè)置這一條語句了
// intent.addCategory("www.morning.categoory");
// 啟動
startActivity(intent);
}
});
}
}
第三步:創(chuàng)建接收隱式啟動的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".goodMorningActivity">
<!-- 你好-->
<TextView
android:textSize="30dp"
android:textColor="@color/purple_700"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好!隱式跳轉(zhuǎn)!"
/>
</LinearLayout>
第四步:AndroidManifest.xml文件的設(shè)置。設(shè)置隱式啟動的組件的名稱。主要包括的是action和category。
代碼如下:
<!-- 測試隱式啟動的案例 -->
<activity
android:name=".goodMorningActivity"
android:exported="true">
<intent-filter>
<!-- 設(shè)置自定義的名稱 -->
<action android:name="www.morning.action" />
<!-- 必須設(shè)置成defaulte -->
<category android:name="android.intent.category.DEFAULT" />
<!-- 可以精確匹配的activity -->
<category android:name="www.morning.categoory" />
</intent-filter>
</activity>
效果圖:
3.3隱式啟動內(nèi)置的組件
1.簡介
- 在缺省情況下,Android系統(tǒng)會調(diào)用內(nèi)置的Web瀏覽器
Intent的語法如下 - Intent構(gòu)造函數(shù)的第1個參數(shù)是Intent需要執(zhí)行的動作
第2個參數(shù)是URI,表示需要傳遞的數(shù)據(jù) - Uri:統(tǒng)一資源標識符,,應(yīng)用程序中的每一種資源起的唯一的名字
2.啟動系統(tǒng)中的組件
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
- Intent的動作是Intent.ACTION_VIEW,根據(jù)URI的數(shù)據(jù)類型來匹配動作
- 數(shù)據(jù)部分使用Uri.parse(urlString)方法,可以簡單的把一個字符串解釋成Uri對象(統(tǒng)一資源標識符).對以Uri方式傳送的數(shù)據(jù),根據(jù)Uri協(xié)議部分以最佳方式啟動相應(yīng)的Activity進行處理。對于http:address將打開瀏覽器查看;對于tel:address將打開撥號呼叫指定的電話號碼
3.Android系統(tǒng)支持的常見動作字符串常量表
?
3.3.1隱式跳轉(zhuǎn)到百度-內(nèi)置組件的使用
第一步:創(chuàng)建主布局文件的代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaiduActivity">
<Button
android:id="@+id/btnbaidu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我進入百度"
android:textSize="30dp"
android:textColor="@color/purple_700"
/>
</LinearLayout>
第二步:創(chuàng)建activity
注意點:Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
第一個參數(shù)設(shè)置是調(diào)用系統(tǒng)的組件。第二個是創(chuàng)建一個要跳轉(zhuǎn)的Uri地址,調(diào)用parse方法將字符串轉(zhuǎn)化成指定的Uri地址。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//設(shè)置隱式啟動打開百度
public class BaiduActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baidu);
// 跳轉(zhuǎn)到百度
Button btnwaidu=findViewById(R.id.btnbaidu);
btnwaidu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳轉(zhuǎn)到百度
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
}
}
效果圖:
3.3.2隱式跳轉(zhuǎn)到撥號-內(nèi)置組件的使用
第一步:創(chuàng)建布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PhoneCallActivity">
<EditText
android:id="@+id/phonenum"
android:maxLength="11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:textSize="30dp"
android:hint="輸入要撥打的電話號碼"
/>
<Button
android:gravity="center_horizontal"
android:id="@+id/btnphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我撥打電話"
android:textSize="30dp"
/>
</LinearLayout>
第二步:編寫activity。
package com.example.activity_study;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
//跳轉(zhuǎn)到打電話的界面
public class PhoneCallActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_call2);
// 跳轉(zhuǎn)到百度
Button btnphone=findViewById(R.id.btnphone);
btnphone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳轉(zhuǎn)到百度
EditText editText=findViewById(R.id.phonenum);
// 獲取要撥打的電話號碼,跳轉(zhuǎn)到撥號的界面
Intent intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+editText.getText()));
startActivity(intent);
}
});
}
}
對于調(diào)用Action_CALL的話需要調(diào)用系統(tǒng)的撥號的權(quán)限,不然是不允許使用的。
效果圖:
文章來源地址http://www.zghlxwxcb.cn/news/detail-409101.html
4.總結(jié)
- 多個Activity之間的調(diào)用
- Intent顯式調(diào)用
- Intent隱式調(diào)用
- 使用Intent在Activity之間傳遞數(shù)據(jù)
- 直接使用Intent傳遞數(shù)據(jù)
- 使用Bundle傳遞數(shù)據(jù)
- 回傳數(shù)據(jù)
到了這里,關(guān)于Activity跳轉(zhuǎn)和Intent傳值的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!