How can I detect focused EditText in android?

2019-01-17 20:05发布

I have a number of EditTexts in my page along with two buttons. I want the user to touch on any one EditText field and click any button to insert a certain value into that very EditText field he touched. Giving input using keypad is not allowed. Please help me to do this.

7条回答
够拽才男人
2楼-- · 2019-01-17 20:12

In your activity or fragment implement OnFocusChangeListener.

edittextNeme= (EditText) findViewById(R.id.edittextNeme);
edittextNeme.setOnFocusChangeListener(this);

You will have to check which view changed focus by getting the view id with view.getId() and handle accordingly.

@Override
public void onFocusChange(View view, boolean hasfocus) {
    switch(view.getId()){
    case R.id.edittextNeme:
    //Do something
    break;

    ...etc
    }
}
查看更多
The star\"
3楼-- · 2019-01-17 20:14

This worked for me.

e1 = (EditText) findViewById(R.id.editText1);

e1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
   @Override
   public void onFocusChange(View arg0, boolean hasfocus) {
      if (hasfocus) {
         Log.e("TAG", "e1 focused") 
      } else {
         Log.e("TAG", "e1 not focused") 
      }            
   }
});
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-17 20:17

You can use View.OnFocusChangeListener to detect if any view (edittext) gained or lost focus.

for (EditText view : editList){
   view.setOnFocusChangeListener(focusListener);
}
....
private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
             focusedView = v;
        } else {
             focusedView  = null;
        }
    }
}

This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class. And obviously you would need to create an array with them in it, thus the editList.

查看更多
叛逆
5楼-- · 2019-01-17 20:18

This works for me. It returns a boolean.

myEditText.hasFocus()
查看更多
Ridiculous、
6楼-- · 2019-01-17 20:32

With Java 8 lambdas:

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

editTextTo.setOnFocusChangeListener((view, b) -> {
    if (view.isFocused()) {
        // Do whatever you want when the EditText is focused 
        // Example:
        editTextFrom.setText("Focused!");
    }
});

Note that the view inside the lambda function is actually an instance of an EditText.

查看更多
Lonely孤独者°
7楼-- · 2019-01-17 20:33

Have your Fragment implement OnTouchListener & OnFocusChangeListener:

public class AttributesFragment extends Fragment 
                       implements OnTouchListener, OnFocusChangeListener {

Then implement using:

@Override
public boolean onTouch(View view, MotionEvent event) {
    if (view instanceof EditText) {
        view.setOnFocusChangeListener(this); // User touched edittext
    }
    return false;
}

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

}

Don't forget to set your listener after creating your EditText

editText.setOnTouchListener(this);

This way if an EditText box is focused by default but user has not changed the field it will not fire the onFocusChange event.

查看更多
登录 后发表回答