gpt4 book ai didi

java - 在 TextView 中显示数组元素

转载 作者:行者123 更新时间:2023-12-04 10:34:40 25 4
gpt4 key购买 nike

****我对 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>

最佳答案

你可以这样做:

public class MainActivity extends AppCompatActivity {

int i = 0;

TextView wordTextView; // declare your textView here

// no need to create array again in "getNextWord" method
String[] array = {"Noun", "Adjective", "Proper Noun", "Name"};

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

// initialise text view here
wordTextView = findViewById(R.id.wordTextView);
}

public void nextWord(View view) { //nextWord is the onclick of the button
i = i++;
getNextWord() // call this method here, without passing string
}

public void getNextWord() {
// you need to check the value of "i", it should not be greater than "array" size.
// you can simply do
// i = i% array.length
// or you can check index with if-condition
// otherwise you will get "ava.lang.ArrayIndexOutOfBoundsException" if i >= array.length
wordTextView.setText(array[i]);
}
}

关于java - 在 TextView 中显示数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60238444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com