Android Studio-如何从文本文件读取(行)?

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

现在我的代码如下:

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的一些解决方案,但我遇到了麻烦,需要您的帮助!

java android text stream filereader
1个回答
0
投票

这是有关FileInputStream,FileOutputStream和BufferedReader的很好的教程。这些示例很容易理解。

https://www.androidauthority.com/lets-build-a-simple-text-editor-for-android-773774/

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