【问题标题】:How to save input from TextEdit field into a string?如何将 TextEdit 字段的输入保存到字符串中?
【发布时间】:2019-03-23 12:09:00
【问题描述】:

我正在制作一个简单的应用程序,其中有一个 TextInput 字段和旁边的一个按钮。 TextInput 字段从 strings.xml 加载 @string/duration_time。 它旁边的按钮将显示一条简单的消息(“hello” + 字符串 duration_time)。但是,它将始终显示来自字符串 duration_time 的原始信息。

如何设置 TextChangedListener 来更新字符串/duration_time 以反映用户的输入?如何从 TextInput 访问信息并使用它?因为在下面的示例中,在“public void afterTextChanged”下我试图保存信息,但我不知道它存储在哪里。

亲切的问候

button2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String duration_input= WHAT_Do_I_Put_Here;
            }
        });

【问题讨论】:

    标签: android string addtextchangedlistener


    【解决方案1】:

    假设您使用 editText 和 button 作为 Android Wiget。 现在,如果您想通过单击按钮获取editText中的文本。可以这样做

    String textEdit = "";
    button.setOnClickListener(->view{
        textEdit = editText.getText().toString();
    });
    

    现在您的 textEdit 将包含您在 editText 中输入的文本。

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 2019-04-06
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2021-06-25
      • 2011-12-19
      相关资源
      最近更新 更多