为什么setAlpha()作用于我所有的按钮,同时setImageResource()作用于只有一个按钮? [英] Why does setAlpha() act on all my buttons whilst setImageResource() acts on just one button?

查看:345
本文介绍了为什么setAlpha()作用于我所有的按钮,同时setImageResource()作用于只有一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的按钮不起作用。前两次是任何pressed所有按钮都影响而不仅仅是一个已经pressed。

交换:

  seatButton [I] .setAlpha(255);

有关:

  seatButton [I] .setImageResource(0x7f020007)

和我的code的作品!只有按钮I preSS的影响。为什么呢?

  @覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    表=新表(); //创建表
    sea​​tButton =新的ImageButton [10]; //创建按钮的数组
    sea​​tStats =新的TextView [10]; //创建用于统计面板阵列
    //创建longClickListener,用于玩家坐下或缩小。
    longClickListener =新View.OnLongClickListener()
    {
        @覆盖
        公共布尔onLongClick(视图V)
        {
            的for(int i = 0;我小于10;我++)
            {
                //每个座位[I]将与每个imageButtoni + 1对应
                如果(v.getId()==(getResources()。则getIdentifier(的ImageButton+第(i + 1),ID,en.deco.android.livehud)))
                {
                    //如果座椅是空的填充它,放置一个玩家在座椅和从半透明改变按钮不透明
                    如果(table.seats [I] .getState()。等于(空))
                    {                        sea​​tButton [I] .setAlpha(255);
                        //seatButton[i].setImageResource(0x7f020000);
                        table.seats [I] .SIT(新播放器());
                        sea​​tStats [I] .setVisibility(View.VISIBLE);
                        Toast.makeText(GUI.this,table.seats [I] .getState(),Toast.LENGTH_SHORT).show();
                    }
                    //如果座位已满,清空
                    其他
                    {
                        sea​​tButton [I] .setAlpha(80);
                        //seatButton[i].setImageResource(0x7f020007);
                        table.seats [I] .sitOut();
                        sea​​tStats [I] .setVisibility(View.INVISIBLE);
                        Toast.makeText(GUI.this,table.seats [I] .getState()+ I,Toast.LENGTH_SHORT).show();
                    }
                }
            }
            返回true;
        }
    };
    //分配按钮和统计在布局XML定义自己的appropiate Java数组面板。同时设置clickListeners到按钮。
    的for(int i = 0;我小于10;我++)
    {
        sea​​tButton [I] =(的ImageButton)findViewById(getResources()则getIdentifier(的ImageButton+第(i + 1),ID,en.deco.android.livehud)。);
        sea​​tStats [I] =(TextView的)findViewById(getResources()则getIdentifier(TextView的+第(i + 1),ID,en.deco.android.livehud)。);
        sea​​tButton [I] .setOnLongClickListener(longClickListener);
        sea​​tButton [I] .setAlpha(80);
    }


解决方案

您正在做与 == 字符串比较,这意味着你要比较的参考,而不是值。这可能不是你想要的,所以你应该改变,从

 如果(table.seats [I] .getState()==空){...}

 如果(table.seats [I] .getState()。等于(空)){...}

除此之外,根据 setAlpha(浮动阿尔法)(这是API 11的方法,仅供参考)的文档中,通过浮动应<$之间C $ C> [0 ... 1] 。

你设置的图像资源是的ImageManager的 R.id.transparent_background 。这可能表明你的逻辑工作,但误差确实是在某处设置alpha值。

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);
    }

解决方案

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.

这篇关于为什么setAlpha()作用于我所有的按钮,同时setImageResource()作用于只有一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆