Set focus on spinner when selected in android

风格不统一 提交于 2019-11-30 20:36:13

Here's my solution.

mSpinner.setFocusableInTouchMode(true);
mSpinner.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            if (mSpinner.getWindowToken() != null) {
                mSpinner.performClick();
            }
        }
    }
});

Use this to avoid EditText Focus :

scrollView = (ScrollView) findViewById(R.id.scrollView_id);
        scrollView.setOnTouchListener(new OnTouchListener() {
            // to solve foocus problem on scrolling
            public boolean onTouch(View v, MotionEvent event) {
                if (myEditText.hasFocus()) {
                    myEditText.clearFocus();
                }
                if (myEditText1.hasFocus()) {
                    myEditText1.clearFocus();
                }

                return false;
            }
        });

I solved the problem with the help of Ryderz answer. here is the code :

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
scrollView = (ScrollView) findViewById(R.id.sv_main);
    scrollView.setOnTouchListener(new OnTouchListener() {
        // to solve focus problem on scrolling
        public boolean onTouch(View v, MotionEvent event) {

            IBinder windowToken = null;
            if (myEditText1.hasFocus()) {
                myEditText1.clearFocus();
                windowToken = myEditText1.getWindowToken();
            }
            if (myEditText2.hasFocus()) {
                myEditText2.clearFocus();
                windowToken = myEditText2.getWindowToken();
            }
            if (myEditText3.hasFocus()) {
                myEditText3.clearFocus();
                windowToken = myEditText3.getWindowToken();
            }
            if (windowToken != null) {
                imm.hideSoftInputFromWindow(windowToken, 0);
            }
            scrollView.requestFocusFromTouch();
            return false;
        }
    });

Then I set the android:focusable="true" for my textViews so that on scroll when focus is removed from editText then the Textviews can be picked for focus. In that way the user doesn't see any focused control on screen.

I had the same problem like you and I've resolved with this solution.

I've redefined OnItemSelectedListener and when the spinner changes the value, I take the focus and after that, I release again.

This is my class CustomOnItemSelectedListener:

public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener{

    private Spinner mSpinner;
    private int iCurrentSelection;

    public CustomOnItemSelectedListener(Spinner s){
        mSpinner = s;
        iCurrentSelection = mSpinner.getSelectedItemPosition();
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

        if (iCurrentSelection != arg2) {

            mSpinner.setFocusableInTouchMode(true);
            mSpinner.requestFocus();
            mSpinner.setFocusableInTouchMode(false);
            mSpinner.clearFocus();

        }

        iCurrentSelection = arg2;
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        //No hace falta hacer nada
    }

}

In my code I do this

Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(spinner));

If you want to keep focus, don't do "clearFocus()".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!