Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的全過(guò)程記錄
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) :
- 打開(kāi)App時(shí),啟動(dòng)CreateMessageActivty
加載activity_create_message.xml作為布局 - 用戶(hù)點(diǎn)擊按鈕啟動(dòng)ReceiveMessageActivty
加載activity _receive_message.xml作為布局
- 打開(kāi)App時(shí),啟動(dòng)CreateMessageActivty
1.2、開(kāi)發(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類(lèi):發(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打開(kāi),設(shè)置收件Activity為ReceiveMessageActivity Intent intent = new Intent(this,ReceiveMessageActivity.class) ; //在intent中附加消息 intent.putExtra(MESSAGE_KEY,message); //向Android發(fā)出請(qǐng)求 startActivity(intent); } }
ReceiveMessageActivty類(lèi):接收消息
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) :
- 打開(kāi)App時(shí),啟動(dòng)CreateMessageActivty
加載activity_create_message.xml作為布局 - 用戶(hù)點(diǎn)擊按鈕啟動(dòng)選擇啟動(dòng)滿(mǎn)足條件的App
- 打開(kāi)App時(shí),啟動(dòng)CreateMessageActivty
2.2、開(kāi)發(fā)視圖布局
- activity_create_message.xml
- 同1.2中的activity_create_message.xml
2.3、按鈕事件響應(yīng)
CreateMessageActivty類(lèi):發(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中的類(lèi)名 Intent intent = new Intent(Intent.ACTION_SEND); //設(shè)置消息類(lèi)型為純文本,系統(tǒng)不會(huì)對(duì)消息進(jìn)行處理 intent.setType("text/plain"); //向Intent添加附加信息 intent.putExtra(Intent.EXTRA_TEXT,message); //自定義選擇對(duì)話(huà)框 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)頁(yè)面跳的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kotlin開(kāi)發(fā)中與if等價(jià)的takeIf與takeUnless詳解
這篇文章主要介紹了Kotlin開(kāi)發(fā)中與if等價(jià)的takeIf與takeUnless使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11詳解Android中Service服務(wù)的基礎(chǔ)知識(shí)及編寫(xiě)方法
這篇文章主要介紹了詳解Android中Service服務(wù)的基礎(chǔ)知識(shí)及編寫(xiě)方法,包括Service的啟動(dòng)流程及生命周期等基本內(nèi)容,需要的朋友可以參考下2016-04-04詳解Dagger2在Android開(kāi)發(fā)中的新用法
本篇文章主要介紹了Dagger2在Android開(kāi)發(fā)中的新用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Flutter利用Canvas繪制精美表盤(pán)效果詳解
這篇文章主要介紹了如何利用Flutter中的Canvas繪制一個(gè)精美的表盤(pán)效果,文中的實(shí)現(xiàn)步驟講解詳細(xì),快跟隨小編一起動(dòng)手嘗試一下2022-03-03淺談Android中關(guān)于靜態(tài)變量(static)的使用問(wèn)題
本文主要介紹了Android中關(guān)于靜態(tài)變量(static)的使用問(wèn)題,具有一定的參考作用,下面跟著小編一起來(lái)看下吧2017-01-01Android庫(kù)項(xiàng)目中的資源ID沖突的解決方法
本篇文章主要介紹了Android庫(kù)項(xiàng)目中的資源ID沖突的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android 滑動(dòng)監(jiān)聽(tīng)的實(shí)例詳解
這篇文章主要介紹了Android 滑動(dòng)監(jiān)聽(tīng)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本能幫助到大家,需要的朋友可以參考下2017-09-09