gpt4 book ai didi

java - 如何使用按钮显示字符串并更改为下一个字符串(列表中)?

转载 作者:行者123 更新时间:2023-12-02 11:16:30 25 4
gpt4 key购买 nike

我正在制作一个应用程序,它显示字符串,并使用下一个按钮转到下一个字符串。但我的方法根本不显示该字符串。我需要使用 setText 方法吗?

 <TextView
android:id="@+id/fact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="246dp"
android:text=" "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header" />

Activity 代码

public class FunFactsActivity extends AppCompatActivity {
Button back;
Button next;
TextView fact;
private List<String> listOfFacts;

@Override
protected void onCreate( Bundle savedInstanceState) {
setContentView(R.layout.activity_fun_facts);
super.onCreate(savedInstanceState);
fact = findViewById(R.id.fact);
back = findViewById(R.id.back);
next = findViewById(R.id.next);
listOfFacts = new ArrayList<>();

//adds facts to the listOfFacts.
for (int i = 0; i < new Database().facts.length; i++) {
listOfFacts.add(new Database().facts[i]);
}
final String firstFact = listOfFacts.get(0);

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nextFact;
nextFact = firstFact+1;
displayFact(nextFact);
}
});
}
public String displayFact(String fact){
return fact;
}// display all of the facts one at a time, in order, go to the next one using the "next" button.

}

最佳答案

这是你的问题:

public String displayFact(String fact){
return fact;
}

更改为:

public void displayFact(String fact){
this.fact.setText(fact);
}

另外,声明一个计数器作为类的成员来跟踪您的事实:

private int counter;

@Override
protected void onCreate( Bundle savedInstanceState) {
...
counter = 0;

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter += 1
String nextFact = listOfFacts.get(counter);
displayFact(nextFact);
}
});

关于java - 如何使用按钮显示字符串并更改为下一个字符串(列表中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50238257/

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