开发者

Event on EditText

I have one edittext name say eid ..now what i want is as soon as user enter eid value ..an another edittext say "o开发者_StackOverflow社区id" now oid should automatically generate it's value out of the value entered in eid ..so which event should i use to perform above task.. plz help i am very new in android


You can use textWatcher on Edit Text, it gets all the changes in Edit Text, and triggers appropriate functions.

eid.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {

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

    }

    public void afterTextChanged(Editable s) {
        // set oid value now
        oid.setText(eid.getText().toString());
    }
});

Whenever the text will change of "eid" method afterTextChanged will be called and it will set the value of oid also.


if you have two edittextbox with two different names for ex. et1 and et2 then you can write the event handlers for them like

et1= (EditText) findViewById(R.id.EditText01);
et2= (EditText) findViewById(R.id.editText1);


et1.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {


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

    }

    public void afterTextChanged(Editable s) {

    }
});


et2.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub
    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});


Here is a blog-post on how to get "change" events on an EditText field. Once you get the value from the eid, just throw it into the oid.


I think what you are asking for is an OnFocusChangeListener. Create a private subclass in your activity like that:

public MyActivity extends Activity {

  private class MyFocusChangeListener implements OnFocusChangeListener {
    private EditText editText;

    public MyFocusChangeListener(EditText editText) {
      this.editText = editText;
    }

    @Override
    public void onFocusChange(View view, boolean isFocused) {
      if (!isFocused) {
        // View lost focus - now do your stuff
      }
    }
  }

  @Override
  public void onCreate(Bundle bundle) {
    // ...
    editText.setOnFocusChangeListener(new MyFocusChangeListener(editText));
    // ...
  }
}


if you are new to android, why dont you go through some tutorials first... You can also use a button in front of edittext and when user presses that button, you can do whatever you want

probably add text change listner on eid:

eid.addTextChangeListner(...)

One of the function needs to be implemented is:

afterTextChanged(s: Editable){
    // generate value and set in oid
    oid.setText(generateSome(s.toString()))
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜