为什么 setAlpha() 作用于我的所有按钮,而 setImageResource() 只作用于一个按钮?
目前我的按钮不起作用。前两次按下任何按钮时,所有按钮都会受到影响,而不仅仅是已按下的按钮。
交换:
seatButton[i].setAlpha(255);
为:
seatButton[i].setImageResource(0x7f020007)
我的代码有效!只有我按下的按钮才会生效。为什么?
@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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
==
进行字符串比较,这意味着您正在比较引用而不是值。这可能不是你想要的,所以你应该将其从: 更改为:
除此之外,根据 setAlpha(float alpha) 的文档(这是 API 11 方法,仅供参考),传递的浮点值应在
[0...1]
之间。您要设置的图像资源是 ImageManager 的
R.id.transparent_background
。这可能表明您的逻辑有效,但错误确实存在于设置 alpha 值的某个地方。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:to:
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.我找到了一个解决方案,但我不理解它也不满意。我设置了一个 alpha 动画,在打开程序时将所有按钮的 alpha 值从 255 更改为 255。换句话说,它什么也不做。不过我的程序现在可以运行了。我欢迎更好的解决方案或解释为什么会这样?
此代码与其余初始化一起放置在 onCreate() 方法的开头。它设置一个保持相同值的 AlphaAnimation。
这与我的问题底部显示的循环相同,只是更改了一行。在将我的所有按钮设置为半透明之前,它会激活此固定动画。如果没有此动画,所有按钮都会受到单击的影响。通过动画,每个按钮在且仅被单击时才会响应。
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.
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.