標籤:
Main_layout.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"> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/myet" android:textSize="40dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通方式傳到第二頁" android:textSize="30dp" android:onClick="onclick" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帶返回方式傳到第二頁" android:textSize="30dp" android:onClick="onclick3" /></LinearLayout>
MainActivity.java
package com.example.chenshuai.test321;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.Toast;/** * Created by chenshuai on 2016/3/21. */public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); } //普通方式 public void onclick(View view) { Log.e("tag","按鈕的點擊監聽觸發的"); //靜態方法 //不許要執行個體化,直接用類名調用 //構建了Toast //方法鏈 Toast.makeText(this,"按鈕的點擊監聽觸發的",Toast.LENGTH_LONG).show(); //Toast.makeText(this,"按鈕的點擊監聽觸發的",Toast.LENGTH_SHORT).show(); /*Toast ta = Toast.makeText(this,"按鈕的點擊監聽觸發的",Toast.LENGTH_LONG); ta.show();*/ //取得要傳遞的資訊 //擷取View執行個體 EditText myet = (EditText)findViewById(R.id.myet); String str = myet.getText().toString(); Intent intent = new Intent(this,Activity2.class); //儲存內容 //Extra 擴充 實際上是一個HashMap,進行限制 putExtra 是一個bundle intent.putExtra("myet",str); //啟動,不需要傳回值 startActivity(intent); } //帶傳回值的方式 public void onclick3(View view) { EditText myet = (EditText)findViewById(R.id.myet); String str = myet.getText().toString(); Intent intent = new Intent(this,Activity2.class); //儲存內容 //Extra 擴充 實際上是一個HashMap,進行限制 putExtra 是一個bundle intent.putExtra("myet",str); //啟動方式,有傳回值 //第一個參數 intent //第二個參數 requestCode 請求碼 startActivityForResult(intent,1); } //處理返回資訊的監聽(回調方法) //監聽所有返回資訊的 //必須要有requestCode區分由哪個請求返回的 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if (resultCode == RESULT_OK) { //擷取返回資訊 String str = data.getExtras().getString("mytv"); //顯示 EditText myett = (EditText)findViewById(R.id.myet); myett.setText(str); //Toast.makeText(this,"返回資訊成功",Toast.LENGTH_LONG); } else { Toast.makeText(this,"返回資訊有問題",Toast.LENGTH_LONG); } } }}
activity_2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.chenshuai.test321.Activity2"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:textSize="40dp" android:id="@+id/mytv" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通返回" android:textSize="40dp" android:onClick="onclick1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帶傳回值返回" android:textSize="40dp" android:onClick="onclick2" /></LinearLayout>
Activity2.java
package com.example.chenshuai.test321;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); //接受資訊 //擷取意圖 //傳遞過來的Intent Intent in = getIntent(); String s = in.getExtras().getString("myet"); EditText mytv = (EditText)findViewById(R.id.mytv); mytv.setText(s); } //普通返回 public void onclick1(View view) { //關閉當前Activity finish(); } public void onclick2(View view) { //儲存返回資料 也要用Intent EditText mytv = (EditText)findViewById(R.id.mytv); Intent in = new Intent(); //設定返回資料 //先設定ResultCode,再設定儲存資料的意圖 setResult(RESULT_OK,in.putExtra("mytv",mytv.getText().toString())); finish(); }}
AndroidMinifest.xml
package com.example.chenshuai.test321;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); //接受資訊 //擷取意圖 //傳遞過來的Intent Intent in = getIntent(); String s = in.getExtras().getString("myet"); EditText mytv = (EditText)findViewById(R.id.mytv); mytv.setText(s); } //普通返回 public void onclick1(View view) { //關閉當前Activity finish(); } public void onclick2(View view) { //儲存返回資料 也要用Intent EditText mytv = (EditText)findViewById(R.id.mytv); Intent in = new Intent(); //設定返回資料 //先設定ResultCode,再設定儲存資料的意圖 setResult(RESULT_OK,in.putExtra("mytv",mytv.getText().toString())); finish(); }}
Android——關於Activity跳轉的返回(無傳回值和有傳回值)——總(Test)