來源:初次學習,建議直接看這個官方原版,我這個是總結出來的要點,供自己日後複習,參考
文章目錄
- 1. 響應信息效果圖
- 2.步驟
- 2.1 Add an EditText and a Button to the SecondActivity layout (略)
- 2.2 Create a response Intent in the second Activity
- 1.添加 鍵 define the key for the Intent extra:
- 2.添加成員變量,hold 住一個 `EditText`
- 3. find 發現`EditText`
- 4. 獲取 信息
- 5. 在`returnReply ()`方法中 new 出一個 Intent 對象
- 6. 將reply作爲附加信息添加到new出來的 Intent(鍵值對格式)
- 7. 使用 `setResult`聲明已經返回成功
- 8. 結束 (`finish()`)
- 2.3 顯示載體 Add TextView elements to display the reply
- 2.4 讀取 Get the reply from the Intent extra and display it
- 1. 定義一個特殊的鍵 key
- 2. 成員變量
- 3. find 發現 `findViewByID()` to get references
- 4. 把 `startActivity` 改爲`startActivityForResult()` 包括 鍵key
- 5. 重寫 onActivityResult()
- 6. Inside onActivityResult(), call super.onActivityResult()
- 7. test
- 8. 獲取信息
- 9. 設置可見性
- 10 . Set the reply TextView text to the reply, and set its visibility to true:
1. 響應信息效果圖
2.步驟
2.1 Add an EditText and a Button to the SecondActivity layout (略)
目的是輸入響應的內容,點擊Button
後迴應,XML代碼見後面附加。
2.2 Create a response Intent in the second Activity
同樣的道理,信息從second Activity 返回到 main Activity 也是通過 Intent ,sent an Intent extra.
相當於前一步的主體和客體顛倒一下。
打開 Second Activity
1.添加 鍵 define the key for the Intent extra:
At the top of the class, add a public constant to define the key for the Intent extra:
public static final String EXTRA_REPLY =
"com.example.a1104.android.twoactivities.extra.REPLY";
2.添加成員變量,hold 住一個 EditText
Add a private variable at the top of the class to hold the EditText
private EditText mReply;
3. find 發現EditText
In the onCreate() method, before the Intent code, use findViewByID() to get a reference to the EditText and assign it to that private variable:
mReply = findViewById(R.id.editText_second);
4. 獲取 信息
In the returnReply() method, get the text of the EditText as a string:
String reply = mReply.getText().toString();
5. 在returnReply ()
方法中 new 出一個 Intent 對象
In the returnReply() method, create a new intent for the response—don’t reuse the Intent object that you received from the original request.
Intent replyIntent = new Intent();
6. 將reply作爲附加信息添加到new出來的 Intent(鍵值對格式)
Add the reply string from the EditText to the new intent as an Intent extra. Because extras are key/value pairs, here the key is EXTRA_REPLY, and the value is the reply:
replyIntent.putExtra(EXTRA_REPLY, reply);
7. 使用 setResult
聲明已經返回成功
Set the result to RESULT_OK to indicate that the response was successful. The Activity class defines the result codes, including RESULT_OK and RESULT_CANCELLED.
setResult(RESULT_OK,replyIntent);
8. 結束 (finish()
)
finish();
在Second Anctivity 中的代碼的完整版是:
public class activity_second extends AppCompatActivity {
public static final String EXTRA_REPLY =
"com.example.a1104.android.twoactivities.extra.REPLY";
private EditText mReply;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mReply = (EditText)findViewById(R.id.editText_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setText(message);
}
public void returnReply(View view) {
String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
finish();
}
}
2.3 顯示載體 Add TextView elements to display the reply
2.3.2 使用 android:visibility
設置可見性
Add the android:visibility attribute to each TextView to make them initially invisible. (Having them visible on the screen, but without any content, can be confusing to the user.
android:visibility="invisible"
activity_main.xml 中的代碼見附加
2.4 讀取 Get the reply from the Intent extra and display it
如果不希望返回任何值,使用
startActivity()
啓動,僅僅是啓動另一個Activity.
本例中希望返回值,使用startActivityFromResult ()
open MainActivity
1. 定義一個特殊的鍵 key
Add a public constant at the top of the class to define the key for a particular type of response you’re interested in:
public static final int TEXT_REQUEST = 1;
2. 成員變量
Add two private variables to hold the reply header and reply TextView elements:
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
3. find 發現 findViewByID()
to get references
mReplyHeadTextView = findViewById(R.id.text_header_reply);
mReplyTextView = findViewById(R.id.text_message_reply);
完整的代碼如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageEditText = findViewById(R.id.editText_main);
mReplyHeadTextView = findViewById(R.id.text_header_reply);
mReplyTextView = findViewById(R.id.text_message_reply);
}
4. 把 startActivity
改爲startActivityForResult()
包括 鍵key
In the launchSecondActivity() method, change the call to startActivity() to be startActivityForResult(), and include the TEXT_REQUEST key as an argument:
startActivityForResult(intent, TEXT_REQUEST);
5. 重寫 onActivityResult()
@Override
public void onActivityResult(int requestCode,
int resultCode, Intent data) {
}
The three arguments to onActivityResult() contain all the information you need to handle the return data: the requestCode
you set when you launched the Activity with startActivityForResult(),
the resultCode
set in the launched Activity (usually one of RESULT_OK or RESULT_CANCELED),
and the Intent data
that contains the data returned from the launch Activity.
6. Inside onActivityResult(), call super.onActivityResult()
?
super.onActivityResult(requestCode, resultCode, data);
7. test
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
}
}
8. 獲取信息
Inside the inner if block, get the Intent extra from the response Intent (data). Here the key for the extra is the EXTRA_REPLY constant from SecondActivity:
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
9. 設置可見性
mReplyHeadTextView.setVisibility(View.VISIBLE);
10 . Set the reply TextView text to the reply, and set its visibility to true:
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
完整代碼:
@Override
public void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
String reply =
data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
Run
附加
activity_second.xml 中的代碼:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".activity_second">
<TextView
android:id="@+id/text_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_header"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_header" />
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginBottom="36dp"
android:text="@string/button_second"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:onClick="returnReply"/>
<EditText
android:id="@+id/editText_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="36dp"
android:ems="10"
android:hint="@string/editText_second"
android:inputType="textLongMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_second"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
android:autofillHints="" />
</android.support.constraint.ConstraintLayout>
activity_main 中的代碼:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<TextView
android:id="@+id/text_header_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_header_reply"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="invisible"/>
<TextView
android:id="@+id/text_message_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_header_reply"
android:visibility="invisible"/>
<Button
android:id="@+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginBottom="36dp"
android:text="@string/button_main"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:onClick="launchSecondActivity"/>
<EditText
android:id="@+id/editText_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="36dp"
android:ems="10"
android:hint="@string/editText_main"
android:inputType="textLongMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_main"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>
PS完整代碼:
MainActivity.java
package com.example.a1104.twoactivities;
import android.content.Intent;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG =
MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE=
"com.example.a1104.android.twoactivities.extra.MESSAGE";
private EditText mMessageEditText;
public static final int TEXT_REQUEST = 1;
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_TAG,"------");
Log.d(LOG_TAG,"onCreate");
mMessageEditText = findViewById(R.id.editText_main);
mReplyHeadTextView = findViewById(R.id.text_header_reply);
mReplyTextView = findViewById(R.id.text_message_reply);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button clicked!");
Intent intent = new Intent(this,SecondActivity.class);
String message =mMessageEditText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivityForResult(intent,TEXT_REQUEST);
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == TEXT_REQUEST){
if (resultCode == RESULT_OK){
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onStart(){
super.onStart();
Log.d(LOG_TAG,"onStart");
}
@Override
protected void onRestart(){
super.onRestart();
Log.d(LOG_TAG,"onRestart");
}
@Override
protected void onResume(){
super.onResume();
Log.d(LOG_TAG,"onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.d(LOG_TAG,"onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.d(LOG_TAG,"onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d(LOG_TAG,"onDestroy");
}
}
SecondActivity.java
package com.example.a1104.twoactivities;
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;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private static final String LOG_TAG =
SecondActivity.class.getSimpleName();
public static final String EXTRA_REPLY =
"com.example.a1104.twoactivities.extra.REPLY";
private EditText mReply;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mReply =(EditText)findViewById(R.id.editText_second);
Log.d(LOG_TAG, "-------");
Log.d(LOG_TAG, "onCreate");
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setText(message);
}
@Override
protected void onStart(){
super.onStart();
Log.d(LOG_TAG,"onStart");
}
@Override
protected void onRestart(){
super.onRestart();
Log.d(LOG_TAG,"onRestart");
}
@Override
protected void onResume(){
super.onResume();
Log.d(LOG_TAG,"onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.d(LOG_TAG,"onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.d(LOG_TAG,"onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d(LOG_TAG,"onDestroy");
}
public void returnReply(View view) {
String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK,replyIntent);
finish();
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<TextView
android:id="@+id/text_header_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_header_reply"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_message_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_header_reply"
android:layout_marginLeft="8dp" />
<Button
android:id="@+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:text="@string/button_main"
android:onClick="launchSecondActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="16dp" />
<EditText
android:id="@+id/editText_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/editText_main"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@string/button_main"
app:layout_constraintStart_toStartOf="parent"
/>
</android.support.constraint.ConstraintLayout>
second_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".SecondActivity">
<TextView
android:id="@+id/text_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_header"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_header"
android:layout_marginLeft="8dp" />
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:text="@string/button_second"
android:onClick="returnReply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="16dp" />
<EditText
android:id="@+id/editText_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/editText_second"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@string/button_second"
app:layout_constraintStart_toStartOf="parent"
/>
</android.support.constraint.ConstraintLayout>