簡體   English   中英

無法將數據從第二個活動傳遞到主活動

[英]Can't pass data from second Activity to main

我在Activity2和ActivityMain之間傳遞數據時遇到麻煩。 我有一個帶有“添加人”按鈕的ActivityMain。 當我按下此按鈕時,我進入Activity2,然后填寫“名稱”字段並按下按鈕“添加”,但是無法在我的ActivityMain上添加名稱。

ActivityMain.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"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add Person"
    android:onClick="addPerson"
   />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

ActivityMain.java

package com.example.test.zadanie01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; 
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        Intent intent = getIntent();
        String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
        setContentView(textView);
}

public void addPerson(View view){
    Intent intent = new Intent(this, PersonActivity.class);
    startActivityForResult(intent, 1);
}
}

Activity2.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"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/send_name"/>
   <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Person"
        android:onClick="addPersonToMain"
        />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Activity2.java

package com.example.test.zadanie01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class PersonActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person);
}

public void addPersonToMain(View view){
    Intent returnintent = new Intent(this, MainActivity.class);
    EditText SendName = (EditText) findViewById(R.id.send_name);

    String message = SendName.getText().toString();
    returnintent.putExtra(EXTRA_MESSAGE, message);
    setResult(RESULT_OK, returnintent);
    finish();
}
}

更改代碼的這一部分。

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == 1){
            if (resultCode == RESULT_OK){
                String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);

                TextView textView = new TextView(this);
                textView.setTextSize(40);
                textView.setText(message);
                setContentView(textView);
            }
        }
}

您無需在onActivityResult創建另一個Intent ,該Intent已由以下方法傳遞: Intent data 然后將數據用作您的意圖。 並首先檢查它是否是您要求的requestCode,以及結果是否正確。

編輯

從評論中回答問題的另一部分。

1-將TextView添加到您的activity_main.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"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
<TextView
 android:id="@+id/text_message"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="40sp"
 android:visibility="gone" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add Person"
    android:onClick="addPerson"
   />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

2-檢索數據消息時,顯示TextView並顯示內容。

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == 1){
            if (resultCode == RESULT_OK){
                String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);

                TextView textView = findViewById(R.id.text_message);
                textView.setVisibility(View.VISIBLE);
                textView.setText(message);
            }
        }
}

嘗試更換

String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);

String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM