簡體   English   中英

單擊另一個活動的按鈕后修改文本視圖值

[英]Modify Text View values after clicking a button from another activity

我一直在嘗試通過單擊另一個活動來更改 TextView 字段的值。 我嘗試了各種活動生命周期方法。 當我按下后退按鈕時,onRestart() 方法會更改 TextView 值,但我想要的是從另一個活動中更改按鈕單擊時的值。

這是我的 MainActivity

package com.example.activity1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button submitButton;
    Context context;

    @Override
    protected void onRestart() {
        super.onRestart();
        TextView textView = findViewById(R.id.text1);
        textView.setText("Welcome Back");

        Toast.makeText(context, "Restart Started", Toast.LENGTH_SHORT).show();
    }


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

        textView = findViewById(R.id.text1);
        submitButton = findViewById(R.id.submit);
        context = this;

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, Activity2.class));
            }
        });


    }
}

我的第二個活動 Class

    package com.example.activity1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity2 extends AppCompatActivity {

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

        Context context;
        Button reverseButton;

        reverseButton = findViewById(R.id.reverse);
        context = this;

        reverseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MainActivity.class);


                startActivity(intent);
            }
        });
    }
}

我的活動2.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=".Activity2">

    <TextView
        android:id="@+id/textt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity 2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/reverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.592" />
</androidx.constraintlayout.widget.ConstraintLayout>

還有我的 Main_Activity.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=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Page"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.628" />

</androidx.constraintlayout.widget.ConstraintLayout>

您應該將活動 2 中的某些內容傳遞給活動 1,以告訴活動 1 您已經在這里。 由於您使用 Intents putExtra 來執行此操作

Intent intent = new Intent(context, MainActivity.class);
String killroyWasHere= "Welcome Back";
intent.putExtra("iAmBack", killroyWasHere);
startActivity(intent);

在 OnCreate() 的活動 1 中

@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
    String myData = getIntent().getStringExtra("iAmBack");
    if (myData != null) {
        TextView textView = findViewById(R.id.text1);
        textView.setText(myData);
        Toast.makeText(context, "Restart Started", Toast.LENGTH_SHORT).show();
    }
   ...
}

應該可以,在 Android 中沒有使用 Java 多年。

希望我能正確理解你想要做什么,這是有幫助的答案

更多閱讀建議Intents and Intent Filters

暫無
暫無

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

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