【问题标题】:setText android studiosetText 安卓工作室
【发布时间】:2020-06-16 01:43:14
【问题描述】:

screenshot of how it appears所以我正在尝试在 android 上创建一个测验应用程序,但在处理它时,我注意到我用来设置第一个问题和答案的 setText 不起作用,如果你能帮我找到那就太好了找出这里不起作用的东西。 请注意,只有在尝试以编程方式设置文本时才会出现问题

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;       
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Play extends AppCompatActivity {
private Button a1,a2,a3,a4;
private TextView question;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);

    a1 = (Button)findViewById(R.id.a1);
    a2 = (Button)findViewById(R.id.a2);
    a3 = (Button)findViewById(R.id.a3);
    a4 = (Button)findViewById(R.id.a4);
    question = findViewById(R.id.question);
    question.setText("Pick");
    a1.setText("A");
    a2.setText("B");
    a3.setText("C");
    a4.setText("D");
}




<?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"
android:background="@drawable/background"
tools:context=".Play">

<Button
    android:id="@+id/a2"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    android:text=""
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/a1" />

<Button
    android:id="@+id/a3"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    android:text=""
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/a2"
/>

<Button
    android:id="@+id/a4"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.515"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/answer3"
    app:layout_constraintVertical_bias="0.0"
/>

<Button
    android:id="@+id/a1"
    android:layout_width="245dp"
    android:layout_height="50dp"
    android:text=""
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/question"
/>

<TextView
    android:id="@+id/question"
    android:layout_width="364dp"
    android:layout_height="264dp"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp"
    android:text=""
    app:layout_constraintBottom_toTopOf="@+id/a1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.487"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
/>

【问题讨论】:

  • 显示什么错误?
  • 我在帖子中添加了我运行它时得到的屏幕截图,按钮和文本视图是空白的
  • 我认为没有错误。只是你必须重新安排你的布局。
  • 我也看到了这篇文章,我尽可能简单地编写代码以跟踪问题但仍然一无所获,按钮的文本仍然是空白的。还是谢谢

标签: java android-studio


【解决方案1】:

我已经测试了您的代码,其显示结果符合预期。尝试更改按钮文本的文本颜色。或通过设置视图的背景颜色来检查哪个视图首先出现。有时代码正在运行,但由于某些观点对其他人的看法,它不反映输出

【讨论】:

    【解决方案2】:

    尝试添加

    import android.widget.View;
    

    在您的 java 文件的顶部。

    Android widget: How to change the text of a button

    【讨论】:

    • 嗨,我添加了它,但仍然没有变化。我也检查了该帖子,与那里的代码相比,我的代码看起来还不错,很奇怪。
    • 没有意义但是如果你在onCreate中声明Button呢?按钮 a1 = 等等..
    • 我也这样做了,但没有任何改变
    • 检查 findViewById 方法是否正常工作(实现 onClick)。 Logcat 中是否有一些错误?
    • 是的,我认为我的 findViewById 实现了 onClick 应用程序在我单击时关闭
    猜你喜欢
    • 2013-07-08
    • 2017-04-17
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多