標籤:
介紹:
兩個activity進行跳轉,在跳轉過程中,將message由MainActivity傳遞到secondActivity,並且當secondActivity退回至MainActivity時,也傳遞訊息給MainActivity。 首先是MainActivity的布局檔案:activity_main.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="horizontal"> <EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/send_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:onClick="sendMessage" /> <!--定義onclick函數--></LinearLayout>
接下來是MainActivity的實現:MainActivity.java
package com.example.administrator.helloworld;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.EditText;public class MainActivity extends AppCompatActivity { //發送Intent對應字串內容的key public static final String Intent_key="MESSAGE"; //EditText private EditText editText =null; private void initView(){ editText= (EditText) findViewById(R.id.edit_message); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設定布局 setContentView(R.layout.activity_main); initView(); } //發送訊息,啟動secondActivity! public void sendMessage(View view){ Intent intent = new Intent(this,secondActivity.class); String text =editText.getText().toString(); intent.putExtra(Intent_key,text); startActivityForResult(intent,0);//此處的requestCode應與下面結果處理函中調用的requestCode一致 } //結果處理函數,當從secondActivity中返回時調用此函數 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0 && resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); String text =null; if(bundle!=null) text=bundle.getString("second"); Log.d("text",text); editText.setText(text); } }}
請注意區別:在進行activity跳轉的時候,調用變為startActivityForResult(intent,0),並且實現了返回處理函數。其中定義了返回的相關操作。注意在返回處理函數中需要對requestCode以及resultCode進行比對。 緊接著是second_activity的布局檔案:second_activity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="50dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="點擊按鈕返回" /></LinearLayout>
最後是secondActivity.java的實現:
package com.example.administrator.helloworld;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.view.View.OnClickListener;public class secondActivity extends AppCompatActivity { private Button button=null; private TextView textView =null; //設定類ButtonListener實現介面,OnClickListener,在其中可以指定不同id的button對應不同的點擊事件 //可以藉此將代碼抽出來,提高代碼的可閱讀性 private class ButtonListener implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: Intent intent =getIntent(); //這裡使用bundle繃帶來傳輸資料 Bundle bundle =new Bundle(); //傳輸的內容仍然是索引值對的形式 bundle.putString("second","hello world from secondActivity!");//回傳的訊息,hello world from secondActivity! intent.putExtras(bundle); setResult(RESULT_OK,intent); finish(); break; } } } //初始化View public void initView(){ button= (Button) findViewById(R.id.button); textView= (TextView) findViewById(R.id.textView); button.setOnClickListener( new ButtonListener() ); Intent intent =getIntent(); String text =intent.getStringExtra(MainActivity.Intent_key); textView.setText(text); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); initView(); }}
首先注意對Button設定監聽事件的處理,通過類ButtonListener 實現介面OnClickListener實現,在其中通過switch區分不同的Button id,來針對不同的按鈕實現不同的操作。 接著注意利用Bundle對象在activity之間傳遞訊息。在結束activity之前,調用setResult(RESULT_OK,intent),把需要回傳的訊息發送至上一個activity。 :
android入門:activity之間跳轉,並且回傳參數