在 TextView 中显示数组元素
Displaying array elements in a TextView
****我是 Java 和 android 工作室的新手,我正在尝试将数组的单个元素显示到我的 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>
Call your getNextWord() method in nextWord()
public void nextWord(View view) { //nextWord is the onclick of the button
i = i++;
getNextWord();
}
And remove parameter from getNextWord().
你可以这样做:
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 和 android 工作室的新手,我正在尝试将数组的单个元素显示到我的 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>
Call your getNextWord() method in nextWord()
public void nextWord(View view) { //nextWord is the onclick of the button
i = i++;
getNextWord();
}
And remove parameter from getNextWord().
你可以这样做:
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]);
}
}