简体   繁体   中英

How to lock the spinner after button click in android

How can I prevent the user to change the value selected in spinner after some event like button press?

It is like after selecting the desired value from spinner the user will press a button after which they will not be allowed to see the values in the spinner except the top value ie the selected one so that no change be made in the selected value.

UPDATE:- And yes the spinners are also created dynamically on the press of that same button so it should lock only the previously created spinners not the new one.

Code for dynamic creation of Layout which contains the Spinner along with an EditText & a Button :-

 View.OnClickListener addListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);

            newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
            btnRemove.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    container.removeView(newView);
                }
            });

            container.addView(newView);

简单只需在按钮单击上禁用微调器

spinner.setEnabled(false);

In your onClick method, you can just set the visibility to GONE to remove the view as shown below:

View.OnClickListener addListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);

        newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                newView.setVisibility(View.GONE);  // remove (hide) your view
            }
        });

        container.addView(newView);

And to enable it again, you can set its visibility to VISIBLE as shown below:

newView.setVIsibility(View.VISIBLE);

Or you could just show/hide your spinner as shown below:

your_spinner.setVisibility(View.GONE);  // to remove (hide)
your_spinner.setVisibility(View.VISIBLE);  // to make it visible

If you want to only disable the spinner:

your_spinner.setEnabled(false);

Hope it helps.

You need to create a variable (may be boolean flag = true )

On button click change to flag = false

And inside Listener to Spinner check if flag is true of false

Example

//inside listener of spinner

if(flag){
//do task
}else{
//restrict the task or don't do anything or display message
}

For Updated question

Then you should use disable() method. spinner1.disable() or spinner2.disable() or so on..

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