多个(动态)EditText和onFocusChange问​​题

时间:2013-09-10 18:27:13

标签: android validation android-edittext

我在for循环中生成多个EditText,并希望在EditText之一丢失焦点时更新“分数”。我还想检查输入是否有效,如果没有,请清空EditText并将焦点设置回它。

以下代码添加了EditTextsonFocusChangeListener

问题是当分数无效时(updateScore(id)返回false),我想清空EditText并将焦点设置回此视图。

我的代码中的问题是,如果我在EditText A 中输入无效的值,然后点击EditText B A B 都有焦点...我只希望A有焦点......

如何将焦点设置回上一个EditText并准备好接收此视图的新输入?

    for (int player = 0; player < numPlayers; player++)
    {
        EditText valueET = new EditText(this);

         valueET.setOnClickListener(new OnClickListener() {          
         @Override public void onClick(View v) {
               Log.v("Focus", "Lagrer unna den som har blitt klikket, " + String.valueOf(v.getId()));
               etHasFocus = (EditText) v;
           }
         });

        valueET.setOnFocusChangeListener(new OnFocusChangeListener() {          
           public void onFocusChange(View v, boolean hasFocus) {
           if(!hasFocus)
           {
               if(!updateScore(v.getId()))
               {
                 if (etHasFocus != null && v.getId() != etHasFocus.getId()) 
                     etHasFocus.clearFocus();               

                   v.requestFocus();
             }
           });

         valueET.setId(200+player*100+row);
         valueET.setInputType(InputType.TYPE_CLASS_NUMBER);
         valueET.setTextColor(Color.BLACK);
         valueET.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

         tr.addView(valueET);
    }
tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

updateScore(id)的代码是(简化):

boolean updateScore(int id)
{
    EditText t = (EditText) findViewById(id);
    String text = t.getText().toString();
    if( !text.equals(""))
    {
        int score = Integer.parseInt(text);
    }
    if(score != 9)
         return false;

        TextView total = (TextView) findViewById(ID_toal);
        t2.setText(String.valueOf(score));
        return true;
}

代码更新,现在问题是onClick永远不会被调用...(所以etHasFocus = null)

1 个答案:

答案 0 :(得分:1)

您可以尝试跟踪哪个EditText已被点击。为此,创建一个EditText变量:

EditText etHasFocus;

除了添加OnFocusChangeListener之外,还要添加OnClickListener

valueET.setOnClickListener(new OnClickListener() {          

    @Override
    public void onClick(View v) {
        etHasFocus = (EditText) v;
    }
});

现在,在OnFocusChangeListener内,进行以下更改:

valueET.setOnFocusChangeListener(new OnFocusChangeListener() {          

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {

            if(!updateScore(v.getId())) {

                // Clear focus from another EditText
                if (etHasFocus != null && v.getId() != etHasFocus.getId()) {
                    etHasFocus.clearFocus();                       
                }

                v.requestFocus();
            }
        }
    }
});

修改

你是对的。第二次单击时调用onClick(View)方法。我建议你采用另一种方法。我已经尝试过并且工作正常:

创建EditText变量:

EditText etCurrent;

在每个EditText上设置OnTouchlistener

valueET.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            if (etCurrent != null) {
                if (etCurrent.getId() != v.getId()) {

                    if (!check(etCurrent.getId())) {
                        etCurrent.setSelection(0,
                                 etCurrent.getText().toString().length());
                        return true;
                    } else {
                        etCurrent = (EditText) v;
                    }
                }
            } else {
                etCurrent = (EditText) v;
            }
        }

        return false;
    }
});

删除OnClickListeneronFocusChangeListeneretHasFocus

您无需初始化etCurrent,因为if (etCurrent != null) { } else { }可以解决这个问题。