Java 单击时隐藏图像视图

Java 单击时隐藏图像视图,java,android,imageview,Java,Android,Imageview,感谢所有的建议和想法,我最终通过你的建议实现了它,这是一个简单的逻辑工作来解决这个问题。如果有人想创建自定义健康栏,我想与大家分享,但它不适合100 HP,因为这样你就必须编写100条if语句, 其想法是创建简单的定制Healthbar,并通过点击按钮来减少它。 这个链接对我也有很大帮助。 代码 private TextView Message,result; private Button play; private float maxHP = 10; private float current

感谢所有的建议和想法,我最终通过你的建议实现了它,这是一个简单的逻辑工作来解决这个问题。如果有人想创建自定义健康栏,我想与大家分享,但它不适合100 HP,因为这样你就必须编写100条if语句, 其想法是创建简单的定制Healthbar,并通过点击按钮来减少它。 这个链接对我也有很大帮助。 代码

private TextView Message,result;
private Button play;
private float maxHP = 10;
private float currentHP = maxHP;
//private float percentHP =  (float) (currentHP/maxHP);
private int user_shield;
private float healthBar;
private ImageView shieldusb1,shieldusb2,shieldusb3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);   
setContentView(R.layout.activity_level_one);

Initialize();

}


private void Initialize(){ 
   user_shield = R.raw.shield_blue;
   shieldusb2 = (ImageView)  findViewById(R.id.shield_b2);
   shieldusb1 = (ImageView)  findViewById(R.id.shield_b1);
   shieldusb3 = (ImageView)  findViewById(R.id.shield_b3);
   result = (TextView) findViewById(R.id.result);
   play = (Button) findViewById(R.id.playButton);
   play.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            play();
}
   });

}

public void play(){
    {         
        healthBar=0;
        currentHP = (float) (currentHP - 0.5);       

        //currentHP -= 1;
    if(currentHP <= 0) //if the player died
    {
         currentHP = 0; //set his HP to zero, (just in case)
         Message.setText("You died! :P"); //add any extra death-code here
         //update the healthBar
         result.setText ("WIN POINTS"); 
    }
    updateHealthBar();
  }

    }
 private void updateHealthBar() {   
    // percentHP =  currentHP / maxHP;
     healthBar = currentHP;


     if(healthBar<9.6){
            shieldusb1.setVisibility(View.GONE); 
     }
     {               
     if (healthBar<=9){
            shieldusb2.setVisibility(View.GONE);
       }
     if (healthBar<=8.6){
        shieldusb3.setVisibility(View.GONE);
         }
     }

     }
私有文本查看消息,结果;
私人按钮播放;
专用浮点数maxHP=10;
专用浮点电流hp=maxHP;
//私有浮动百分比hp=(浮动)(当前hp/maxHP);
专用int用户屏蔽;
私人浮动健康吧;
私有图像视图屏蔽B1、屏蔽B2、屏蔽B3;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u level\u one);
初始化();
}
私有void Initialize(){
用户屏蔽=R.raw.shield\u蓝色;
shieldusb2=(ImageView)findViewById(R.id.shield_b2);
屏蔽b1=(图像视图)findViewById(R.id.shield_b1);
shieldusb3=(ImageView)findViewById(R.id.shield_b3);
结果=(TextView)findViewById(R.id.result);
play=(按钮)findViewById(R.id.playButton);
play.setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
play();
}
});
}
公共游戏{
{         
healthBar=0;
currentHP=(浮动)(currentHP-0.5);
//电流hp-=1;

如果(当前HP您需要调整这些条件

if(healthBar<=1)
...
else if (healthBar<=0.9)

if(healthBar我猜,您需要删除else if并使其成为if,因为如果执行中的第一个语句,您将检查healthBar是否,这是唯一的逻辑问题。更改if/if的if/else if块

 if(healthBar<=1)
    shieldusb1.setVisibility(View.GONE);                
 if (healthBar<=0.9)
    shieldusb2.setVisibility(View.GONE);

如果(healthBar您的
healthBar
int类型,那么检查0.9与检查1是一样的

此外,在您的情况下,您首先要检查:

if (healthBar <= 1)

if(healthBar)什么逻辑问题。告诉他。你好,Melq,通过删除else并使用“if”,两个图像在一次单击中隐藏,而不是在条件满足时隐藏,因为您的变量是int,而且,如果您进行计算,当按下按钮时,
healthBar
是0,因此两个条件都满足,因为0
if (healthBar <= 1)