Why does setAlpha() act on all my buttons whilst setImageResource() acts on just one button?

二次信任 提交于 2019-12-11 16:08:14

问题


At the moment my buttons do not work. The first two times any are pressed all buttons are influenced rather than just the one that has been pressed.

Swap:

seatButton[i].setAlpha(255);

For:

seatButton[i].setImageResource(0x7f020007)

And my code works! Only the button I press is effected. Why?

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    table = new Table(); //Creates Table
    seatButton = new ImageButton[10]; //Creates Array of buttons
    seatStats = new TextView[10]; //Creates array for stat panels


    //Creates longClickListener, used for players to sit in or out.
    longClickListener = new View.OnLongClickListener() 


    {


        @Override
        public boolean onLongClick(View v) 
        {
            for(int i=0; i<10; i++)
            {
                //Each seat[i] will correspond with each imageButtoni+1
                if(v.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"))) 
                {
                    //If the seat is empty fill it, place a player in the seat and change the button from translucent to opaque
                    if(table.seats[i].getState().equals("empty"))
                    {

                        seatButton[i].setAlpha(255);
                        //seatButton[i].setImageResource(0x7f020000);
                        table.seats[i].sit(new Player());
                        seatStats[i].setVisibility(View.VISIBLE);
                        Toast.makeText(GUI.this, table.seats[i].getState(), Toast.LENGTH_SHORT).show();
                    }
                    //If the seat is full, empty it
                    else
                    {
                        seatButton[i].setAlpha(80);
                        //seatButton[i].setImageResource(0x7f020007);
                        table.seats[i].sitOut();
                        seatStats[i].setVisibility(View.INVISIBLE);
                        Toast.makeText(GUI.this, table.seats[i].getState() + i, Toast.LENGTH_SHORT).show();
                    }
                }
            }
            return true;
        }
    };


    //Assigns the buttons and stats panels defined in the layout xml to their appropiate java arrays. Also sets clickListeners to buttons.
    for(int i = 0; i < 10; i++)
    {
        seatButton[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"));
        seatStats[i] = (TextView) findViewById(getResources().getIdentifier("textView" + (i+1), "id", "en.deco.android.livehud"));
        seatButton[i].setOnLongClickListener(longClickListener);
        seatButton[i].setAlpha(80);
    }

回答1:


You're doing a String comparison with ==, which means you're comparing references and not values. This is probably not what you want, so you should change that from:

if(table.seats[i].getState() == "empty") { ... }

to:

if(table.seats[i].getState().equals("empty")) { ... }

Besides that, according to the documentation of setAlpha(float alpha) (which is an API 11 method, just for reference), the passed float should be between [0...1].

The image resource you're setting is the ImageManager's R.id.transparent_background. This may suggest that your logic works, but the error is indeed somewhere in setting the alpha value.




回答2:


I've found a solution yet I do not understand it nor am I happy with it. I set up an alpha animation that changed the alpha value of all my buttons from 255 to 255 upon opening my program. In other words it does nothing. However my program now works. I would welcome a better solution or an explanation as to why this works?

This code is placed with the rest of initializations at the start of the onCreate() method. It sets up an AlphaAnimation that remain at the same value.

//Initializes AlphaAnimations to alter transparency
alphaDown = new AlphaAnimation(1f, 1f); 
alphaDown.setDuration(1000);
alphaDown.setFillAfter(true);

This is the same loop shown at the bottom of my question with one line changed. It activates this stationary animation before setting all my buttons as translucent. Without this animation all buttons are affected with one click. With the animation each button responds when it and only it has been clicked.

        //Assigns the buttons and stats panels defined in the layout xml to their appropiate java arrays. Also sets clickListeners to buttons.
    for(int i = 0; i < 10; i++)
    {
        seatButton[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"));
        seatStats[i] = (TextView) findViewById(getResources().getIdentifier("textView" + (i+1), "id", "en.deco.android.livehud"));
        seatButton[i].setOnLongClickListener(longClickListener);
        seatButton[i].startAnimation(alphaDown);
        seatButton[i].setAlpha(80);
    }


来源:https://stackoverflow.com/questions/8022980/why-does-setalpha-act-on-all-my-buttons-whilst-setimageresource-acts-on-just

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