Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the value in EditText field?

Tags:

android

I have three EditText, I want to concatinate the strings present in the first two EditText fields,and display in the third EditText field.After entering the string at 2nd field,it automatically concatinate and set in the third EditText.

EditText text1 = (EditText) findViewById(R.id.text1);
mtext1=text1.getText.toString();

EditText text2 = (EditText) findViewById(R.id.text2);
mtext2 = text2.getText.toString();


mtext3=mtext1.concat().mtext2;
Edit text3 = (EditText) findViewById(R.id.text3);
text3 = setText(mtext3.toString());

I wrote the above code.But I result is not shomn in the third EditText. Please give the solution, that I implement in my program

like image 858
Tripaty Sahu Avatar asked Mar 01 '11 18:03

Tripaty Sahu


People also ask

How do I change my EditText value?

This example demonstrates how do I set only numeric value for editText in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you edit text explain?

In android, EditText is a user interface control which is used to allow the user to enter or modify the text. While using EditText control in our android applications, we need to specify the type of data the text field can accept using the inputType attribute.


2 Answers

This should work. Make sure you do not edit text2 in the TextChanged listener because then afterTextChanged would get called again.

final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);

text2.addTextChangedListener(new TextWatcher() {
    void afterTextChanged(Editable s) {
        text3.setText(text1.getText().toString() + text2.getText().toString());
    };
});
like image 122
Robby Pond Avatar answered Oct 12 '22 12:10

Robby Pond


If you want to detect when your two EditText fields change, you're going to need to use addTextChangedListener() on each of them. The following can go in your onCreate() method:

final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);

TextWatcher watcher = new TextWatcher() {
    void afterTextChanged(Editable s) {
        text3.setText(text1.getText() + text2.getText());
    };
});

text1.addTextChangedListener(watcher);
text2.addTextChangedListener(watcher);
like image 30
Matthew Willis Avatar answered Oct 12 '22 14:10

Matthew Willis