有没有一种全局方法来捕获 Android 中所有 EditText 焦点变化?

Is there a global way to catch all EditText focus changes in Android?

有没有什么巧妙的方法可以避免下面的第一块代码重复十几次?第二个块的形式与第一个相同,我还有几个具有相同形式的块。我正在考虑 EditText 字段的数组(好主意?坏 [为什么?]?)但是是否有 global 方法来制作 one 块 捕捉焦点的所有变化?

   txtExclude.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       public void onFocusChange(View v, boolean hasFocus) 
       {
           if (!hasFocus)
               if (txtExclude.getText().length() == 0)
                   txtExclude.setBackgroundColor(Color.WHITE);

       }});

   txtRequired.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       public void onFocusChange(View v, boolean hasFocus) 
       {
           if (!hasFocus)
               if(txtRequired.getText().length() == 0)
                   txtRequired.setBackgroundColor(Color.WHITE);
       }});

编辑

第一个答案的非工作实施:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnFocusChangeListener
{
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState); // call superclass's version
      setContentView(R.layout.activity_main); // inflate the GUI

      final EditText txtPattern = (EditText) findViewById(R.id.txtPattern);

       final EditText txtLegal = (EditText) findViewById(R.id.txtLegal);

       final EditText txtExclude = (EditText) findViewById(R.id.txtExclude);

       final EditText txtRequired = (EditText) findViewById(R.id.txtRequired);

       EditText txtLetters = (EditText) findViewById(R.id.txtLetters);

   } // end method onCreate

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        return; // breakpoint here **********************
    }
} // end class MainActivity

无论哪个 EditText 在调试过程中获得或失去焦点,都没有到达断点。

如果您想处理焦点的所有变化,您可能想要实现 OnFocusChangeListener

类似于:

public class MainActivity extends Activity implements OnFocusChangeListener
{

    @Overrride
    public void onFocusChange(View v, boolean hasFocus)
    {
     //check which view changed and do some stuff
    }

}

这是另一种方式。

通过子class像下面的 EditText

在你的包中创建一个 CustomEdittext class

让你的包裹是com.android.app

public class CustomEditText extends EditText {

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {

    if (!focused)
        if (getText().length() == 0)
            setBackgroundColor(Color.WHITE);

    super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}

您可以在布局中使用此 customEditText,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.android.app.CustomEditText
    android:id="@+id/com.example.customedittext1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Name"
    android:inputType="textPersonName" >
</com.android.app.CustomEditText>

<com.android.app.CustomEditText
    android:id="@+id/edEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Email"
    android:inputType="textPersonName" >
</com.android.app.CustomEditText>

<com.android.app.CustomEditText
    android:id="@+id/edContact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Contact"
    android:inputType="textPersonName" >
</com.android.app.CustomEditText>

您不需要在每个编辑字段上设置 onFocusChangedListner()。

希望对您有所帮助

@Qadir Hussain 几周前就解决了我的问题(时间过得真快)。这是我在 activity_main.xml 中实现它的方式,并进行了额外的调整:

<com.dslomer64.wordyhelperton.CustomEditText
    android:id="@+id/txtRequired"
     ...        
    android:hint="@string/required_hint"
    >
    <requestFocus />
</com.dslomer64.wordyhelperton.CustomEditText>

MainActivity.java中:

static CustomEditText txtExclude;
static CustomEditText txtRequired;
...
...onCreate...

  txtExclude   = (CustomEditText) findViewById(R.id.txtExclude);
  txtRequired  = (CustomEditText) findViewById(R.id.txtRequired);

CustomEditText.java中:

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

public class CustomEditText extends EditText
{
  public CustomEditText(Context context, AttributeSet attrs)
  {
    super(context, attrs);
  }

  @Override
  protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
  {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (getText().length() == 0) setBackgroundColor(Color.WHITE);
    else                         setBackgroundColor(Color.YELLOW);
  }

  public void onTextChanged(CharSequence s, int start, int before, int count)
  {
    super.onTextChanged(s, start, before, count);
    if (getText().length() == 0) setBackgroundColor(Color.WHITE);
    else                         setBackgroundColor(Color.YELLOW);
  }
}
  1. 您可以使用 ViewTreeObserver 并在根视图布局上添加 onGlobalFocusChange 侦听器。下面是示例代码:

(在这里放一行8个空格将格式化第一行代码。)

    findViewById(R.id.yourRootContainer).getViewTreeObserver().addOnGlobalFocusChangeListener
    (
       new ViewTreeObserver.OnGlobalFocusChangeListener() 
       {
            @Override
            public void onGlobalFocusChanged(View oldFocus, View newFocus) 
            {
                whatever();
            }
       }
    );
  1. 您可以使用自定义焦点侦听器,使用视图 ID 进行区分。下面是示例代码:

    private class MyFocusChangeListener implements 
    View.OnFocusChangeListener 
    {
        @Override
        public void onFocusChange(View view, boolean hasFocus) 
        {
            if (view.getId() == R.id.my_view_id) 
            {
                doSomethingHere();
            }
        }
    }
    

并将其用作:

myView1.setOnFocusChangeListener(new MyFocusChangeListener());

收听 activity 的 window 中视图的所有焦点变化。

window.decorView.viewTreeObserver.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
    println("old=$oldFocus new=$newFocus")
}