Android實(shí)現(xiàn)頁面跳轉(zhuǎn)的全過程記錄
1、啟動(dòng)新Activty
1.1、功能分析
- App功能
- 在第一個(gè)Activity輸入消息
- 點(diǎn)擊第一個(gè)Activity的發(fā)送按鈕
- 發(fā)送消息到第二個(gè)Activity
- 第二個(gè)Activity顯示收到的消息
- App結(jié)構(gòu)(2個(gè)Activity+2個(gè)Layout) :
- 打開App時(shí),啟動(dòng)CreateMessageActivty
加載activity_create_message.xml作為布局 - 用戶點(diǎn)擊按鈕啟動(dòng)ReceiveMessageActivty
加載activity _receive_message.xml作為布局
- 打開App時(shí),啟動(dòng)CreateMessageActivty
1.2、開發(fā)視圖布局
activity_create_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".CreateMessageActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint"
android:inputType="textPersonName"
android:textSize="30sp"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSendMessage"
android:text="@string/send"
android:textSize="30sp"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity _receive_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_constraintRight_toRightOf="parent"
tools:context=".ReceiveMessageActivity">
<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd Activity"
android:textSize="34sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constr
string.xml
<resources>
<string name="app_name">Messager</string>
<string name="send">Send Message</string>
<string name="hint">Enter a message</string>
<string name="choser">Send Message via ...</string>
</resources>
1.3、按鈕事件響應(yīng)
CreateMessageActivty類:發(fā)送消息
public class CreateMessageActivity extends AppCompatActivity {
//定義常量,作為消息的key
public static final String MESSAGE_KEY="szst.it.ping.messager";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
public void onSendMessage(View Button){
//獲得編輯框引用
EditText editText = findViewById(R.id.input);
//取出編輯框文字
String message = editText.getText().toString();
//Intent是Android中的信使,新建Intent打開,設(shè)置收件Activity為ReceiveMessageActivity
Intent intent = new Intent(this,ReceiveMessageActivity.class) ;
//在intent中附加消息
intent.putExtra(MESSAGE_KEY,message);
//向Android發(fā)出請(qǐng)求
startActivity(intent);
}
}
ReceiveMessageActivty類:接收消息
public class ReceiveMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_message);
//獲得intent的引用
Intent intent = getIntent();
//根據(jù)key取出value
String message = intent.getStringExtra(CreateMessageActivity.MESSAGE_KEY);
//獲得文本框內(nèi)容,設(shè)置文字
TextView textView = findViewById(R.id.output);
textView.setText(message);
}
}
1.4、測(cè)試結(jié)果
啟動(dòng)界面

輸入消息“123”并點(diǎn)擊按鈕發(fā)送,接收界面如下
2、啟動(dòng)其他App
2.1、功能分析
- App功能
- 在第一個(gè)Activity輸入消息
- 點(diǎn)擊第一個(gè)Activity的發(fā)送按鈕
- 發(fā)送消息到其他App
- 其他App顯示收到的消息
- App結(jié)構(gòu)(1個(gè)Activity+1個(gè)Layout) :
- 打開App時(shí),啟動(dòng)CreateMessageActivty
加載activity_create_message.xml作為布局 - 用戶點(diǎn)擊按鈕啟動(dòng)選擇啟動(dòng)滿足條件的App
- 打開App時(shí),啟動(dòng)CreateMessageActivty
2.2、開發(fā)視圖布局
- activity_create_message.xml
- 同1.2中的activity_create_message.xml
2.3、按鈕事件響應(yīng)
CreateMessageActivty類:發(fā)送消息
public class CreateMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
public void onSendMessage(View Button){
//獲得編輯框引用
EditText editText = findViewById(R.id.input);
//取出編輯框文字
String message = editText.getText().toString();
//使用new Intent(Intent.ACTION_SEND)替換new Intent(this, ReceiveMessageActivity.class),不知道其它App中的類名
Intent intent = new Intent(Intent.ACTION_SEND);
//設(shè)置消息類型為純文本,系統(tǒng)不會(huì)對(duì)消息進(jìn)行處理
intent.setType("text/plain");
//向Intent添加附加信息
intent.putExtra(Intent.EXTRA_TEXT,message);
//自定義選擇對(duì)話框
String chooserTitle = getString(R.string.choser);
Intent chosenIntent = Intent.createChooser(intent, chooserTitle);
startActivity(chosenIntent) ;
}
}
2.4、測(cè)試結(jié)果
啟動(dòng)界面同1.4
輸入消息“123”并點(diǎn)擊按鈕發(fā)送,選擇要發(fā)送的app(Messaging)

發(fā)送附加消息到111

發(fā)送成功

總結(jié)
到此這篇關(guān)于Android實(shí)現(xiàn)頁面跳的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)頁面跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
本篇文章主要介紹了android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
淺談android性能優(yōu)化之啟動(dòng)過程(冷啟動(dòng)和熱啟動(dòng))
本篇文章主要介紹了淺談android性能優(yōu)化之啟動(dòng)過程(冷啟動(dòng)和熱啟動(dòng)) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08
Android 系統(tǒng)簽名 keytool-importkeypair的操作步驟
本文介紹了在Android項(xiàng)目中使用系統(tǒng)簽名并打包APK的方法,步驟包括獲取系統(tǒng)簽名文件、修改AndroidManifest.xml、轉(zhuǎn)換簽名文件、配置Android Studio簽名、生成APK文件和打包APK,感興趣的朋友一起看看吧2025-01-01
Android EditText限制輸入字符類型的方法總結(jié)
這篇文章主要介紹了Android EditText限制輸入字符類型的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android仿微信清理內(nèi)存圖表動(dòng)畫(解決surfaceView屏幕閃爍問題)demo實(shí)例詳解
本文通過實(shí)例代碼給大家講解android仿微信清理內(nèi)存圖表動(dòng)畫(解決surfaceView屏幕閃爍問題)的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android Presentation實(shí)現(xiàn)雙屏異顯
這篇文章主要為大家詳細(xì)介紹了Android Presentation實(shí)現(xiàn)雙屏異顯,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
Android開發(fā)中amera2 Preview使用詳解
這篇文章主要介紹了Android開發(fā)中amera2 Preview使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Flutter 移動(dòng)程序安全性提高的八個(gè)建議
這篇文章主要為大家介紹了Flutter 移動(dòng)程序安全性提高建議詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

