是否有全局方法可以捕获Android中的所有EditText焦点更改?

时间:2015-08-29 17:18:34

标签: java android lost-focus

是否有一种聪明的方法可以避免重复第一段代码十几次?第二个块的形式与第一个块相同,我还有几个具有相同的形式。我正在考虑一系列EditText字段(好主意?不好[为什么?]?)但是有一种全局方法可以使一个块抓住所有焦点变化

   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获得或失去了焦点,都未达到断点。

5 个答案:

答案 0 :(得分:4)

  1. 您可以使用ViewTreeObserver并在根视图布局上添加onGlobalFocusChange侦听器。以下是示例代码:
  2. (在这里放置一个带有8个空格的行将格式化第一行代码。)

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
            return FBSDKApplicationDelegate.sharedInstance().application(app,
                                                                         open: url,
                                                                         sourceApplication: options[.sourceApplication] as! String,
                                                                         annotation: options[.annotation])
        }
    
    1. 您可以使用自定义焦点侦听器,使用视图ID区分。以下是示例代码:

          findViewById(R.id.yourRootContainer).getViewTreeObserver().addOnGlobalFocusChangeListener
          (
             new ViewTreeObserver.OnGlobalFocusChangeListener() 
             {
                  @Override
                  public void onGlobalFocusChanged(View oldFocus, View newFocus) 
                  {
                      whatever();
                  }
             }
          );
      
    2. 并将其用作:

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

答案 1 :(得分:2)

如果您想要处理焦点中的所有更改,您可能希望实现OnFocusChangeListener

类似的东西:

public class MainActivity extends Activity implements OnFocusChangeListener
{

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

}

答案 2 :(得分:2)

这是另一种方式。

通过继承EditText(如下所示)

,在包中创建一个CustomEditext类

让您的包裹为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()。

希望这有帮助

答案 3 :(得分:1)

@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);
  }
}

答案 4 :(得分:0)

收听活动窗口中视图的所有焦点更改。

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