为什么setAlpha()作用于我所有的按钮,同时setImageResource()作用于只有一个按钮?

编程入门 行业动态 更新时间:2024-10-22 14:29:40
本文介绍了为什么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的方法,仅供参考)的文档中,通过浮动应 [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()作用于只有一个按钮?

本文发布于:2023-10-25 13:47:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作用于   按钮   我所   只有一个   setAlpha

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!