目前我的按钮不起作用。前两次是任何pressed所有按钮都影响而不仅仅是一个已经pressed。
交换:
seatButton [I] .setAlpha(255);有关:
seatButton [I] .setImageResource(0x7f020007)和我的code的作品!只有按钮I preSS的影响。为什么呢?
@覆盖公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 表=新表(); //创建表 seatButton =新的ImageButton [10]; //创建按钮的数组 seatStats =新的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()。等于(空)) { seatButton [I] .setAlpha(255); //seatButton[i].setImageResource(0x7f020000); table.seats [I] .SIT(新播放器()); seatStats [I] .setVisibility(View.VISIBLE); Toast.makeText(GUI.this,table.seats [I] .getState(),Toast.LENGTH_SHORT).show(); } //如果座位已满,清空 其他 { 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(); } } } 返回true; } }; //分配按钮和统计在布局XML定义自己的appropiate Java数组面板。同时设置clickListeners到按钮。 的for(int i = 0;我小于10;我++) { 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); }解决方案
您正在做与 == 字符串比较,这意味着你要比较的参考,而不是值。这可能不是你想要的,所以你应该改变,从
如果(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()作用于只有一个按钮?
发布评论