【问题标题】:Android Studio - How to read (Line) from a text file?Android Studio - 如何从文本文件中读取(行)?
【发布时间】:2020-04-04 12:29:25
【问题描述】:

现在我的代码是这样的:

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]); 
                }
      }

它工作正常,但困扰我的是字符串数组。我更喜欢将该对话框保存在文本文件中,并在单击该按钮时逐行读取。 我看到了一些关于资产和 FileReader/BuffedReader 的解决方案,但我被困住了,需要你的帮助!

【问题讨论】:

    标签: java android text stream filereader


    【解决方案1】:

    这里有一个关于 FileInputStream、FileOutputStream 和 BufferedReader 的好教程。这些例子很容易理解。

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

    【讨论】:

    • 我知道如何在普通 Java 应用程序中读取文件并从文本文件中读取一行,但是我在 Android Studio 中遇到了问题。如何实现读取 onClick 行的 FileReader?
    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2014-10-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多