开发者

Swapping data between EditText in Android

开发者 https://www.devze.com 2023-03-26 04:08 出处:网络
I have 2 editText objecst in my android application. I want write to such a logic so that if I type something in textField 1, then same should be replicated in Textfield 2 in runtime and vice-versa.

I have 2 editText objecst in my android application. I want write to such a logic so that if I type something in textField 1, then same should be replicated in Textfield 2 in runtime and vice-versa.

Example: if i type "a" in field1, field2 will also have "a". If I type "b" in field2, field should also change to "b"

I am able to achieve the same one way i,e f开发者_开发百科rom 1 to 2 or 2 to 1 but not simultaneously. Below is the code I wrote:

final EditText tf1 = (EditText)findViewById(R.id.editText1);
final EditText tf2 = (EditText)findViewById(R.id.editText2);

tf1.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
    public void onFocusChange(View v, boolean hasFocus) 
    {
        if (hasFocus)
        {
           tf1.addTextChangedListener(new TextWatcher()
           {
              public void afterTextChanged(Editable s) {tf2.setText(s);}
           } 
        }
     }  
};

Similar way I wrote another listener for tf2 and set the text in tf1. Kindly help on how can this scenario can be handled


Your answer is almost there. The key here is to remove the textChangedListener for either of the EditText fields if it loses focus. Then, keep track of which EditText field currently has the listener, and update the other accordingly. Here's a simple activity that should accomplish what you're looking for:

public class EditTextActivity extends Activity implements TextWatcher {

    private EditText mEditText1;
    private EditText mEditText2;
    private int mEditTextWatcherNum = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mEditText1 = (EditText) findViewById(R.id.editText1);
        mEditText2 = (EditText) findViewById(R.id.editText2);

        mEditText1.addTextChangedListener(this);
        mEditText1.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mEditText1.addTextChangedListener(EditTextActivity.this);
                    mEditTextWatcherNum = 1;

                } else {
                    mEditText1.removeTextChangedListener(EditTextActivity.this);
                }
            }
        });

        mEditText2.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mEditText2.addTextChangedListener(EditTextActivity.this);
                    mEditTextWatcherNum = 2;

                } else {
                    mEditText2.removeTextChangedListener(EditTextActivity.this);
                }
            }
        });


    }

    @Override
    public void afterTextChanged(Editable s) {
        switch (mEditTextWatcherNum) {
        case 1:
            mEditText2.setText(s);
            break;
        case 2:
            mEditText1.setText(s);
            break;
        default:
            break;
        }

    }

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

    }

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

    }

}


Use a global variable to store the previous value when setting the text for the first edit text. Then when setting the text in the second edit text use the value in the global variable.


Should you not remove text changed listener from TextView that looses focus? This will prevent your app from entering an endless loop and updating both TextView's continuously.

0

精彩评论

暂无评论...
验证码 换一张
取 消