具有相同setOnEditorActionListener的多个EditText

时间:2015-02-09 17:43:37

标签: android android-edittext

我可以在某个时间使用我的应用程序" x" EditText字段。我添加了dinamicaly新的EditText,因此它们的数量可以变化。我希望我的所有EditText都有一个监听器,可以告诉我们哪个视图(edittext)已被更改。

我搜索了一下,发现:

 private TextWatcher textWatcher = new TextWatcher()

但问题是它没有参数View v告诉我当前哪个视图改变了:

OnFocusChangeListener listener = new OnFocusChangeListener() .....public void onFocusChange(final View v, ....

还找到了:

OnEditorActionListener

有一个:

public boolean onEditorAction(TextView v, (parammeter)

但是当用户输入时,它不会触发,完成输入......

是否有一个可以用于我所有编辑文本的监听器(没有编号)并且有一个View v参数计?

抱歉我的英文......

任何帮助都会很好

2 个答案:

答案 0 :(得分:1)

OnEditorAction是监听完成和完成键盘操作的合适方法。

尝试使用此方法:

@Override
 public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
 {  
    if ( actionId == EditorInfo.IME_ACTION_DONE )
    return(true);
 }

其中params是:

v           The view that was clicked.
actionId    Identifier of the action. This will be either the identifier you supplied, or EditorInfo.IME_NULL if being called due to the enter key being pressed.
event       If triggered by an enter key, this is the event; otherwise, this is null.

答案 1 :(得分:0)

我找到了一个更适合我的解决方案:

我创建了一个类:

public class Whowho implements TextWatcher
{
   private View view;
   private Activity act;
   public Whowho(EditText view, Activity activ) 
   {
      this.view = view;
      this.act=activ;
   }
   @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) 
   {
       EditText ed = (EditText) act.findViewById(view.getId());
       ....
       AsyncHttpClient client = new AsyncHttpClient();
       RequestParams params = new RequestParams();
       .....
       ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(R.drawable.ok, 0, 0, 0);
       ((TextView) view).setText(...);

}     }

我这样用它:

ed.addTextChangedListener(new Whowho(ed,CLASS.this));

对我来说它很棒。希望它能帮助其他人