Pregunta

I would like to send edit text to a specific string, so that each time the user types something in to the edit text, whatever the user types is sent to that same string each time. Now the string represents whatever the user has typed into that specific edit text box. I would now use this string to display the string in a text view. Is this possible and how could you do this?

Basically I also want the text in the edit text to be identical to that of the text in the text view as well.

code examples of what I'm trying to do:

EditText AValue = (EditText) view.findViewById(R.id.editText1);
AValue.setText( R.string.EditTextInput );

//What I want to do: Whatever User Types is always = String R.string.EditTextInput 
//User makes input equal a String when they press a button, which brings to new activity with text views

text view activity

private void populatescheduleList() {
        myschedule.add(new schedule_view("G Band", R.string.EditTextInput));
¿Fue útil?

Solución

You can do the following:

yourEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
       yourString = s.toString();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

    } 
});

Otros consejos

You can use a TextWatcher to listen to text changes in an EditText. This TextWatcher subclass takes a TextView and each time the String in the EditText changes it sets the new text to the TextView.

public class EditTextWatcher implements TextWatcher {

    private final TextView target;

    private EditTextWatcher(TextView target) {
        this.target = target;
    }

    @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) {
        this.target.setText(s);
    }
}

You would us it like this:

editText.addTextChangedListener(new EditTextWatcher(textView));

Of course you have to be careful with listeners like this. Don't forget to remove the TextWatcher later if you have to!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top