【问题标题】:getText().toString() not working, returns emptygetText().toString() 不工作,返回空
【发布时间】:2017-04-27 14:39:33
【问题描述】:

我正在尝试保存userName,但保存的文本文件总是返回, 6。我怎样才能让它显示输入EditText 的用户名的任何值,以及其余的?例如Don, 6。我已阅读您必须使用 getText() 但这不会在保存的文件中返回任何内容。

但是,如果我将6 替换为从以前的活动中接收分数的意图,则此方法有效!像这样……

        Bundle extras = getIntent().getExtras();
        int score = extras.getInt("Score");

所以这就变成了……

public void addListenerToEndButton() {

    quit = (Button) findViewById(R.id.endBtn);
    userName = (EditText) findViewById(R.id.userName);

    Bundle extras = getIntent().getExtras();
    int score = extras.getInt("score");

    quit.setOnClickListener(new View.OnClickListener() {

        String strName = userName.getText().toString();

        @Override
        public void onClick(View v) {
            saveProgress(strName + ", " + score, "results.txt");
            finish();
            System.exit(0);
        }
    });
}

但它仍然返回empty, whatever score is。例如, 4

我读过这篇文章,建议它应该在 onClickListener 里面: EditText getText().toString() not working

这是我的saveProgress 班级:

public void saveProgress(String contents, String fileName) {

    try {
        File fp = new File(this.getFilesDir(), fileName);
        FileWriter out = new FileWriter(fp);
        out.append(contents);
        out.append("\n\r");
        out.close();
    }

    catch (IOException e) {
        Log.d("Me","file error:" + e);
    }
}

【问题讨论】:

  • 不是您在onclick方法之外检索edittext的文本的问题吗?尝试在 onClick 内移动它

标签: android android-edittext filewriter


【解决方案1】:

我猜你理解错了'inside onClickListener'。您在 atm 所做的是在创建侦听器时读取 strName,但我猜您想在单击 quit 时读取它。

所以只要把线移到函数中,值就会正确。

public void addListenerToEndButton() {

    quit = (Button) findViewById(R.id.endBtn);
    userName = (EditText) findViewById(R.id.userName);

    quit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String strName = userName.getText().toString();
            saveProgress(strName + ", " + 6, "results.txt");
            finish();
            System.exit(0);
        }
    });
}

【讨论】:

    【解决方案2】:

    使用以下内容更改您的 onClick() 方法:

    public void addListenerToEndButton() {
      quit = (Button) findViewById(R.id.endBtn);
      userName = (EditText) findViewById(R.id.userName);
    
      Bundle extras = getIntent().getExtras();
      int score = extras.getInt("score");
    
      quit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String strName = userName.getText().toString();
            saveProgress(strName + ", " + score, "results.txt");
            finish();
            System.exit(0);
        }
      });
    }
    

    调用、初始化、操作、exc 应该放在监听器的onClick 方法中。 onClick 仅在单击按钮时触发,onClick 之外但Listener 内部的所有内容都会在Listener 初始化时调用

    【讨论】:

    • 谢谢,这么简单的忽略。我现在明白了。我知道这超出了问题的范围,但是您知道为什么它会覆盖文件而不是作为新行附加到该文本文件中吗?我在saveProgress 课堂上尝试过。
    • @kamil1995b 老实说我不知道​​,但考虑看看this link :)
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多