[英]Android Studio My Button on the second Activity doesn't Work
I think it a very easy Question.我认为这是一个非常简单的问题。 I am new to Android Studio and I could not find an answer.
我是 Android Studio 的新手,我找不到答案。
I have an activity that switches per Button to another activity.我有一个活动,可以将每个按钮切换到另一个活动。 However, on my second activity, my simple Button just to change the Textview which doesn't work.
但是,在我的第二个活动中,我的简单 Button 只是为了更改不起作用的 Textview。
Layout布局
<?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=".StartActivity">
<TextView
android:id="@+id/twText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="127dp"
android:layout_marginLeft="127dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="127dp"
android:layout_marginRight="127dp"
android:layout_marginBottom="624dp"
android:text="Ihr Passwort war Richtig!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnChecke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="134dp"
android:layout_marginLeft="134dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="189dp"
android:layout_marginRight="189dp"
android:layout_marginBottom="371dp"
android:text="Check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
onCreate
method in my second activity我的第二个活动中的
onCreate
方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
tw = findViewById(R.id.twText);
btnCheck = findViewById(R.id.btnChecke);
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tw.setText("work");
}
});
}
And this is how I am starting the second activity ( onCreate
method from my first activity).这就是我开始第二个活动的方式(第一个活动中的
onCreate
方法)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
tw = findViewById(R.id.twText);
Button btnCheck = findViewById(R.id.btnChecke);
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView("Work"R.layout.activity_main);
}
});
}
I have checked the variable names in the layout and looks like everything is fine.我已经检查了布局中的变量名称,看起来一切都很好。
Thank you in advance.先感谢您。
This answer is based on the problem which I found before the latest edit from the OP.这个答案基于我在 OP 的最新编辑之前发现的问题。 Here is the version of the edit where I found the problem.
这是我发现问题的编辑版本。
First of all, something is very wrong about the onClickListener
of your button.首先,您的按钮的
onClickListener
有一些问题。 You are setting the content view again, instead of starting your second activity.您正在再次设置内容视图,而不是开始您的第二个活动。 It looks like, you are just setting the content view of your
StartActivity
which actually does not launch the MainActivity
that you are trying to start.看起来,您只是在设置
StartActivity
的内容视图,而实际上并没有启动您尝试启动的MainActivity
。
In order to start a new activity, you need to add the following in your button onClickListener
.为了开始一个新的活动,你需要在你的按钮
onClickListener
添加以下内容。
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// setContentView(R.layout.activity_main); // This is not the way for starting another activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
Once your MainActivity
has started, you should be able to change the text with no problem.一旦您的
MainActivity
启动,您应该能够毫无问题地更改文本。
Bundle bundle = getIntent().getExtras()
if(bundle.getString("changeTextView")
TextView .... = (TextView) ....setText();
Hi.你好。 If I good understand, you should use Intent with "putExtra()".
如果我很好理解,您应该将 Intent 与“putExtra()”一起使用。 Exmple:
intent.putExtras("changeTextView");
intent.putExtras("changeTextView");
: intent.putExtras("changeTextView");
After that in your onCreate() method you can put:之后,在您的 onCreate() 方法中,您可以放置:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.