我认为这是一个非常简单的问题。我是Android Studio的新手,找不到答案。
我有一个将每个Button切换到另一个活动的活动。但是,在我的第二个活动中,我简单的Button只是用来更改Textview,这是行不通的。
布局
<?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
方法
@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");
}
});
}
这就是我开始第二个活动的方式(第一个活动中的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);
}
});
}
我已经检查了布局中的变量名称,看起来一切都很好。
谢谢你。
此答案基于我在OP进行最新编辑之前发现的问题。这是我发现问题的version of the edit。
首先,关于按钮的onClickListener
有点错误。您正在重新设置内容视图,而不是开始第二个活动。看起来,您只是在设置StartActivity
的内容视图,而实际上并没有启动您尝试启动的MainActivity
。
为了开始新的活动,您需要在按钮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);
}
});
一旦您的MainActivity
开始,您应该可以毫无问题地更改文本。
Bundle bundle = getIntent().getExtras()
if(bundle.getString("changeTextView")
TextView .... = (TextView) ....setText();
嗨。据我所知,您应该将Intent与“ putExtra()”一起使用。范例:intent.putExtras("changeTextView");
之后,您可以在onCreate()方法中输入:
从tw声明中删除final。用final关键字声明变量时,其值无法修改。