androidstringaddtextchangedlistener

How to save input from TextEdit field into a string?


I'm making a simple app, where there is one TextInput field and a button next to it. The TextInput field loads @string/duration_time from strings.xml. The button next to it will display a simple message ("hello" + the string duration_time). However, it will always display the original information from string duration_time.

How do I set up a TextChangedListener to update the string/duration_time to reflect the input from the user? How do I access the information from TextInput and use it? Because as in the example below, under "public void afterTextChanged" I am trying to save the information, but I don't know where it's stored.

Kind regards

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

Solution

  • Suppose you are using editText and button as Android Wigets. Now if you want to get the the text inside editText on click of button.It can be done like this

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

    Now your textEdit will contain the text which you entered in your editText.