单击其他活动中的按钮后修改文本视图值



我一直在尝试通过从另一个活动中单击来更改TextView字段的值。我尝试了各种"活动生命周期"方法。当我按下后退按钮时,onRestart((方法会更改TextView值,但我想要的是更改另一个活动中单击按钮时的value。

这是我的MainActivty

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));
}
});

}
}

我的第二个活动类

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);
}
});
}
}

我的Activity2.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

最新更新