在 Android Studio 中通过按钮更改文本视图上的文本时无法解决符号错误
Cannot resolve symbol error in while change text on Text view by button in Android Studio
帮帮我,我正在尝试为数独开发应用程序我卡在了这个我只是 Bsc 第一年的人什么都不知道
这里是我的 Xml TextView 代码
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/change"
android:text="Not Changed"/>
这是我的 XML 按钮代码
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Test Button"
app:layout_constraintBottom_toTopOf="@+id/btn_sample"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
这是我的 Java 代码
public void btn(View view) {
TextView t=(TextView)findViewById(change);
t.setText("Changed");
}
如果我成功了,它会激励我和我的 CLG 队友
我是菜鸟,抱歉英语不好
提前致谢
更新:
答案已解决
按钮的新代码
<Button
android:id="@+id/btn"
android:onClick="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Test Button"
app:layout_constraintBottom_toTopOf="@+id/btn_sample"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Java
的新更新代码
public void btn(View view) {
TextView t=(TextView)findViewById(R.id.change);
t.setText("Changed");
}
查找由 android:id
XML 属性标识并在 onCreate(Bundle)
中处理的视图。
使用这个
TextView t=(TextView)findViewById(R.id.change);
而不是这个
TextView t=(TextView)findViewById(change);
编辑
如果你想添加你Button
的点击事件,请使用下面的编辑
在你的Button
中添加android:onclick="btn"
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:onclick="btn"
android:text="Test Button"
app:layout_constraintBottom_toTopOf="@+id/btn_sample"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
帮帮我,我正在尝试为数独开发应用程序我卡在了这个我只是 Bsc 第一年的人什么都不知道
这里是我的 Xml TextView 代码
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/change"
android:text="Not Changed"/>
这是我的 XML 按钮代码
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Test Button"
app:layout_constraintBottom_toTopOf="@+id/btn_sample"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
这是我的 Java 代码
public void btn(View view) {
TextView t=(TextView)findViewById(change);
t.setText("Changed");
}
如果我成功了,它会激励我和我的 CLG 队友 我是菜鸟,抱歉英语不好 提前致谢
更新: 答案已解决 按钮的新代码
<Button
android:id="@+id/btn"
android:onClick="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Test Button"
app:layout_constraintBottom_toTopOf="@+id/btn_sample"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Java
的新更新代码public void btn(View view) {
TextView t=(TextView)findViewById(R.id.change);
t.setText("Changed");
}
查找由 android:id
XML 属性标识并在 onCreate(Bundle)
中处理的视图。
使用这个
TextView t=(TextView)findViewById(R.id.change);
而不是这个
TextView t=(TextView)findViewById(change);
编辑
如果你想添加你Button
的点击事件,请使用下面的编辑
在你的Button
android:onclick="btn"
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:onclick="btn"
android:text="Test Button"
app:layout_constraintBottom_toTopOf="@+id/btn_sample"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />