现在我的代码如下:
private ImageButton nextButton;
private TextView textView;
private int stringIndex = 0;
private String[] dialog = {
"You: Hey, how are you?",
"She: I am fine and you?",
"You: I am fine as well!",
"She: Nice to meet you!"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant);
textView = findViewById(R.id.textView);
nextButton = findViewById(R.id.imageButton);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
if (stringIndex == dialog.length-1){
stringIndex = 0;
textView.setText(dialog[stringIndex]);
} else {
textView.setText(dialog[++stringIndex]);
}
}
效果很好,但令我困扰的是String数组。我更喜欢将该对话框保存在文本文件中,并在单击该按钮时逐行阅读。我看到了资产和FileReader / BuffedReader的一些解决方案,但我遇到了麻烦,需要您的帮助!
这是有关FileInputStream,FileOutputStream和BufferedReader的很好的教程。这些示例很容易理解。
https://www.androidauthority.com/lets-build-a-simple-text-editor-for-android-773774/