在TextView中显示数组元素

问题描述 投票:0回答:2

****我对Java和android studio还是很陌生,我试图在每次单击按钮时将数组的单个元素显示到我的textView wordTextView中。当我单击仿真器上的按钮时,没有任何显示。我也尝试过使用for循环,但我似乎也无法使它正常工作。任何帮助将不胜感激****

  public class MainActivity extends AppCompatActivity {


    int i = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    public void nextWord(View view) { //nextWord is the onclick of the button

        i = i++;
    }


 public void getNextWord (String string) {
     String [] array = {"Noun", "Adjective", "Proper Noun", "Name"};
        TextView wordTextView = findViewById(R.id.wordTextView);
        wordTextView.setText(array[i]);


 }


}

这是.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"
    android:onClick="nextWord"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/wordTextView"
        android:layout_width="155dp"
        android:layout_height="153dp"
        android:layout_marginTop="84dp"
        android:gravity="center"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/enterEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="280dp"
        android:ems="10"
        android:hint="Something"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/wordTextView" />

    <Button
        android:id="@+id/nextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:onClick="nextWord"
        android:text="Next"
        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="@+id/enterEditText"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
java android android-studio textview
2个回答
0
投票

在nextWord()中调用getNextWord()方法>

public void nextWord(View view) { //nextWord is the onclick of the button
    i = i++;
getNextWord();
}

并从getNextWord()中删除参数。


0
投票

您可以执行以下操作:

© www.soinside.com 2019 - 2024. All rights reserved.