How to detect that EditText received focus from Android. Not from the end user regarding edit text?

android.widget.EditText can get focus for two reasons:

Case 1 End users relate to the editing text as intended.

Case 2 The system will automatically edit the text.

  • Navigation
  • The first default focus
  • ...

My question is: How to identify case 2 only ? (Event listener?)

The reason I want to define case 2 is this: I want to set the current position, this is the last position if the edit text gets focus on case 2.

EditText.setOnFocusChangeListener for both case1, case2, so it seems like I can't use this.

Thanks!

+5
source share
1 answer

Updated
Implement android.view.View.OnFocusChangeListener in your activity (for example) and set yourView.setOnFocusChangeListener(yourActivity)
If you combine it with OnTouchListener , then you can filter out user touches, since onTouch () is called first - you can set the boolean class element. Make sure reset is logical if focus is lost.

The code should be something like this:

 ... import android.view.View.OnFocusChangeListener; ... public class MainActivity extends Activity implements OnFocusChangeListener, OnTouchListener { boolean userTouchedView; @Override public View onCreateView(...) { ... yourView.setOnFocusChangeListener(this); ... } @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus && !userTouchedView)) { //YOUR CASE 2 } else if(!hasFocus) userTouchedView=false; } @Override public boolean onTouch(final View v, MotionEvent event) { if(v==yourView){ userTouchedView=true; } } } 
+8
source

Source: https://habr.com/ru/post/1206459/


All Articles