简体   繁体   中英

setOnFocusChangeListener not being called in Fragment Android

In my fragment setOnFocusChangeListener() is no being called. What could be the reason? onToch and onClick are working fine. Here's my code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_barcode_detail,
            container, false);
editText_barcode = (EditText) rootView
            .findViewById(R.id.editText_barcode);

editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                            editos = (EditText) v;
                            System.out.println("====onf== "+editos);
                            key.showCustomNumKeyboard(v);
                        } else {
                            key.hideCustomNumKeyboard();
                        }
            }

        });
}

The Problem is the switch case. Remove the switch case and it will work. View's id returned in your FocusChange listener will be of editText_barcode always.So your switchcase is failing since you are trying to match with another id.

try this...

 your activity implement OnFocusChangeListener

 In onCreateView()
 editText_barcode.setOnFocusChangeListener(this);

 @Override
 public void onFocusChange(View v, boolean hasFocus) {
 switch(v.getId()){
 case r.id.editText_barcode:

      //code here

 break;

...etc
    }
}

Focus is generally requested whenever that view is loaded on screen and you need to grab attention of user.For a view to get focus need to add two attribute android:focusable and android:focusableInTouchMode with that View and then requestFocus

 <EditText android:id="@+id/editText_barcode" 
  ...required attributes
  android:focusable="true"
  android:focusableInTouchMode="true"> 
<requestFocus /> 
</EditText>

Also your are setting onFocusChangeListener() as below make sure it is View.onFocusChangeListener() So suggest you to change below code

editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override

to

editText_barcode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override

Also after checking whether hasFocus I would request you to change it to

        Log.d(TAG,"EditTextBarcode has Focus value " + hasFocus)
        if (hasFocus) {
                   Log.d(TAG,"EditTextBarcode has Focus")
                   key.showCustomNumKeyboard(v);
                } else {
                   Log.d(TAG,"EditTextBarcode does not have Focus")
                   key.hideCustomNumKeyboard();
                }

setOnFocusChangeListener is buggy sometimes. Try setOnTouchListener .

editText_barcode = (EditText) rootView
            .findViewById(R.id.editText_barcode);
editText_barcode.clearFocus()
editText_barcode.setOnTouchListener(new OnTouchListener()() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Your code here
            return false;
        }


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM