简体   繁体   中英

How to find whether if user clicked spinner in Android?

In my application I am creating spinner dynamically in code.I want to force user to click on Spinner and change its value/content. Otherwise user should not be able to go to next screen by clicking Next button.

How to do that in Android? Anybody has any idea?

Thanks in Advance.

Rohan

you can use this:

 if (spin.getSelectedItemPosition() < 0) {//Do something}

this means user hasn't selected anything.

Let the Spinner's first value be something like "-please select-".

when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.

you need code, let me know.

调用您的Intent以获取该微调器的Click Listener中的下一个Activity

Create one boolean global variable like..

 boolean isSelect = false;

Now when user select value from spinner then make it isSelect = false . And when user click on NEXT button check condition for isSelect is true or false.

That's it.

You can get the selected item value by using the following method.

         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {    
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                if (spinner.getSelectedItem().toString().equals("YourValue")) {
                    Intent yourIntent = new Intent(this,YourClassName.class);
                    startActivity(yourIntent);
                }
            }    
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

In my case I added an extra item on first position of the list

1."Select Something" 2."Go next" 3."Go Previous" 5.... 6..

then in use

String item=spinnerObject.getSelectedItem ();
    now check if("Select Something".equels(item)){
show some dialog to select anything from spinner
    }else{
send it to next screen
}

Use this code for checking the spinner item is selected or not .

both flags take at class level (Globally).

Boolean temp = false;
Boolean check = false;


spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            if(temp){
                check = true;
            }
            temp = true;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            check = false;
        }
});

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(check){

                //perform when user select spinner item             
            }else{

                //put Dialog for alert please select spinner item 

            }
        }
}

I made a new Spinner class encapsulating the above mentioned principles. But even then you have to make sure to call the correct method and not setSelection

Same thing in a gist

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;

/**
 * Used this to differentiate between user selected and prorammatically selected
 * Call {@link Spinner#programmaticallySetPosition} to use this feature.
 * Created by vedant on 6/1/15.
 */
public class Spinner extends android.widget.Spinner implements AdapterView.OnItemSelectedListener {

    OnItemSelectedListener mListener;

    /**
     * used to ascertain whether the user selected an item on spinner (and not programmatically)
     */
    private boolean mUserActionOnSpinner = true;

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        if (mListener != null) {

            mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner);
        }
        // reset variable, so that it will always be true unless tampered with
        mUserActionOnSpinner = true;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        if (mListener != null)
            mListener.onNothingSelected(parent);
    }

    public interface OnItemSelectedListener {
        /**
         * <p>Callback method to be invoked when an item in this view has been
         * selected. This callback is invoked only when the newly selected
         * position is different from the previously selected position or if
         * there was no selected item.</p>
         *
         * Impelmenters can call getItemAtPosition(position) if they need to access the
         * data associated with the selected item.
         *
         * @param parent The AdapterView where the selection happened
         * @param view The view within the AdapterView that was clicked
         * @param position The position of the view in the adapter
         * @param id The row id of the item that is selected
         */
        void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected);

        /**
         * Callback method to be invoked when the selection disappears from this
         * view. The selection can disappear for instance when touch is activated
         * or when the adapter becomes empty.
         *
         * @param parent The AdapterView that now contains no selected item.
         */
        void onNothingSelected(AdapterView<?> parent);
    }

    public void programmaticallySetPosition(int pos, boolean animate) {
        mUserActionOnSpinner = false;
        setSelection(pos, animate);
    }

    public void setOnItemSelectedListener (OnItemSelectedListener listener) {
        mListener = listener;
    }

    public Spinner(Context context) {
        super(context);
        super.setOnItemSelectedListener(this);
    }

    public Spinner(Context context, int mode) {
        super(context, mode);
        super.setOnItemSelectedListener(this);
    }

    public Spinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setOnItemSelectedListener(this);
    }

    public Spinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        super.setOnItemSelectedListener(this);
    }

    public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) {
        super(context, attrs, defStyle, mode);
        super.setOnItemSelectedListener(this);
    }
}

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