Java Android onClickListener只工作一次

Java Android onClickListener只工作一次,java,android,eclipse,Java,Android,Eclipse,所以,我对android编程非常陌生。我想制作一个带有三个“攻击”按钮的简单应用程序,当用户按下一个按钮时,它应该计算伤害,更改一个玩家的hp并输出一些文本(谁伤害了谁多少)。一切都正常,但只是第一次。在第一次按下按钮后,应用程序不再响应按钮按下 MainActivity.java package sn.nujno.bitkadolgcasa; import java.util.Random; import android.app.Activity; import android.os.Bu

所以,我对android编程非常陌生。我想制作一个带有三个“攻击”按钮的简单应用程序,当用户按下一个按钮时,它应该计算伤害,更改一个玩家的hp并输出一些文本(谁伤害了谁多少)。一切都正常,但只是第一次。在第一次按下按钮后,应用程序不再响应按钮按下

MainActivity.java

package sn.nujno.bitkadolgcasa;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends Activity implements View.OnClickListener {
Random rand = new Random();
public int hp1 = 100;
public int hp2 = 100;
public int power = 10;
public int igralec = 1;
public String text = "";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button nn = (Button)findViewById(R.id.nn);
    Button hn = (Button)findViewById(R.id.hn);
    Button mn = (Button)findViewById(R.id.mn);
    nn.setOnClickListener(this);
    hn.setOnClickListener(this);
    mn.setOnClickListener(this);
    }

    @Override        
    public void onClick(View v) { 
        int id = v.getId();
        int napad;
        int moznost;
        setContentView(R.layout.activity_main);
        TextView hpT=(TextView)findViewById(R.id.textView3);
        TextView hpA=(TextView)findViewById(R.id.textView4);
        TextView mw=(TextView)findViewById(R.id.mw);

        if(id == R.id.nn && igralec == 1){
        moznost = rand.nextInt(100);
            if(moznost >= 50){
                napad = rand.nextInt(10)+ 1;
                hp1 = hp1 - napad;
                text ="Tim je poskodoval Alena za " + napad + " zivljensjkih tock. Kaj bo storil Alen?" ;
            }else if(moznost < 50){
                text ="Tim je zgresil. Kaj bo storil Alen?";
        }
       igralec = 2;
       }

        else if(id == R.id.nn && igralec == 2){
        moznost = rand.nextInt(100);
            if(moznost >= 50){
                napad = rand.nextInt(10)+ 1;
                hp1 = hp1 - napad;
                text ="Alen je poskodoval Tima za " + napad + " zivljensjkih tock. Kaj bo storil Tim?" ;
            }else if(moznost < 50){
                text ="Alen je zgresil. Kaj bo storil Tim?";
            }
       igralec = 1;
       }

        else if(id == R.id.mn && igralec == 1){
            moznost = rand.nextInt(100);
            if(moznost >= 80){
                napad = rand.nextInt(15)+ 10;
                hp1 = hp1 - napad;
                text ="Tim je poskodoval Alena za " + napad + " zivljensjkih tock. Kaj bo storil Alen?" ;
            }else if(moznost < 80){
                text ="Tim je zgresil. Kaj bo storil Alen?";
            }
       igralec = 2;
       }

        else if(id == R.id.mn && igralec == 2){
            moznost = rand.nextInt(100);
            if(moznost >= 80){
                napad = rand.nextInt(15)+ 10;
                hp1 = hp1 - napad;
                text ="Alen je poskodoval Tima za " + napad + " zivljensjkih tock. Kaj bo storil Tim?" ;
            }else if(moznost < 80){
                text ="Alen je zgresil. Kaj bo storil Tim?";
            }
       igralec = 1;
       }

        else if(id == R.id.hn && igralec == 1){
            moznost = rand.nextInt(100);
            if(moznost >= 25){
                napad = rand.nextInt(5) +1;
                hp1 = hp1 - napad;
                text ="Tim je poskodoval Alena za " + napad + " zivljensjkih tock. Kaj bo storil Alen?" ;
            }else if(moznost < 25){
                text ="Tim je zgresil. Kaj bo storil Alen?";
        }
       igralec = 2;
       }

        else if(id == R.id.hn && igralec == 2){
            moznost = rand.nextInt(100);
            if(moznost >= 25){
                napad = rand.nextInt(5) +1;
                hp1 = hp1 - napad;
                text ="Alen je poskodoval Tima za " + napad + " zivljensjkih tock. Kaj bo storil Tim?" ;
            }else if(moznost < 25){
                text ="Alen je zgresil. Kaj bo storil Tim?";
        }
       igralec = 1;
       }

    else if(hp1 ==0){
        text = "Zmagal je Alen. Igra se bo začela znova.";
        hp1 = 100;
        hp2 = 100;
        igralec = 1;
        }
    else if(hp2 == 0){
        text = "Zmagal je Tim. Igra se bo začela znova.";
        hp1 = 100;
        hp2 = 100;
        igralec = 1;
        }

    mw.setText(text);
    hpT.setText(""+ hp1);
    hpA.setText(""+ hp2);
   }
}
包sn.nujno.bitkadolgcasa;
导入java.util.Random;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
公共类MainActivity扩展活动实现View.OnClickListener{
Random rand=新的Random();
公共int hp1=100;
公共int hp2=100;
公共整数功率=10;
公共int igralec=1;
公共字符串text=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮nn=(按钮)findViewById(R.id.nn);
按钮hn=(按钮)findViewById(R.id.hn);
按钮mn=(按钮)findViewById(R.id.mn);
nn.setOnClickListener(此);
hn.setOnClickListener(本);
mn.setOnClickListener(this);
}
@凌驾
公共void onClick(视图v){
int id=v.getId();
内纳帕德;
int莫兹诺斯特;
setContentView(R.layout.activity_main);
TextView hpT=(TextView)findViewById(R.id.textView3);
TextView hpA=(TextView)findViewById(R.id.textView4);
TextView mw=(TextView)findViewById(R.id.mw);
如果(id==R.id.nn&&igralec==1){
moznost=rand.nextInt(100);
如果(moznost>=50){
napad=兰特·耐克斯汀(10)+1;
hp1=hp1-napad;
text=“Tim je poskodoval Alena za”+napad+“zivljensjkih tock.Kaj bo storil Alen?”;
}否则如果(moznost<50){
text=“Tim je zgresil.Kaj bo storil Alen?”;
}
igralec=2;
}
else if(id==R.id.nn&&igralec==2){
moznost=rand.nextInt(100);
如果(moznost>=50){
napad=兰特·耐克斯汀(10)+1;
hp1=hp1-napad;
text=“Alen je poskodoval Tima za”+napad+“zivljensjkih tock.Kaj bo storil Tim?”;
}否则如果(moznost<50){
text=“Alen je zgresil.Kaj bo storil Tim?”;
}
igralec=1;
}
else if(id==R.id.mn&&igralec==1){
moznost=rand.nextInt(100);
如果(moznost>=80){
napad=兰特·耐克斯汀(15)+10;
hp1=hp1-napad;
text=“Tim je poskodoval Alena za”+napad+“zivljensjkih tock.Kaj bo storil Alen?”;
}否则如果(moznost<80){
text=“Tim je zgresil.Kaj bo storil Alen?”;
}
igralec=2;
}
else if(id==R.id.mn&&igralec==2){
moznost=rand.nextInt(100);
如果(moznost>=80){
napad=兰特·耐克斯汀(15)+10;
hp1=hp1-napad;
text=“Alen je poskodoval Tima za”+napad+“zivljensjkih tock.Kaj bo storil Tim?”;
}否则如果(moznost<80){
text=“Alen je zgresil.Kaj bo storil Tim?”;
}
igralec=1;
}
else if(id==R.id.hn&&igralec==1){
moznost=rand.nextInt(100);
如果(moznost>=25){
napad=兰特·耐克斯汀(5)+1;
hp1=hp1-napad;
text=“Tim je poskodoval Alena za”+napad+“zivljensjkih tock.Kaj bo storil Alen?”;
}否则如果(moznost<25){
text=“Tim je zgresil.Kaj bo storil Alen?”;
}
igralec=2;
}
else if(id==R.id.hn&&igralec==2){
moznost=rand.nextInt(100);
如果(moznost>=25){
napad=兰特·耐克斯汀(5)+1;
hp1=hp1-napad;
text=“Alen je poskodoval Tima za”+napad+“zivljensjkih tock.Kaj bo storil Tim?”;
}否则如果(moznost<25){
text=“Alen je zgresil.Kaj bo storil Tim?”;
}
igralec=1;
}
else if(hp1==0){
text=“Zmagal je Alen.Igra se bo začela znova。”;
hp1=100;
hp2=100;
igralec=1;
}
else if(hp2==0){
text=“Zmagal je Tim.Igra se bo začela znova。”;
hp1=100;
hp2=100;
igralec=1;
}
mw.setText(文本);
hpT.setText(“+hp1”);
hpA.setText(“+hp2”);
}
}
卸下

setContentView(R.layout.activity_main);
从您的
onClick()

您正在扩展相同的布局,而新的布局实例没有安装onclick侦听器。

onclick(视图v)
中调用
setContentView(R.layout.activity\u main)
将替换
活动中已有的所有按钮及其
onClickListeners

像这样重写
onCreate
方法(并声明字段变量):

并从
onClick
方法中删除以下行:

setContentView(R.layout.activity_main);
TextView hpT=(TextView)findViewById(R.id.textView3);
TextView hpA=(TextView)findViewById(R.id.textView4);
TextView mw=(TextView)findViewById(R.id.mw);

这是为了提高效率。
findViewById
调用非常耗时,而且find
视图在单击之间不会更改-您可以在
onCreate
中找到它一次,它与您在if条件中传递的igralec有关…onClick()每次都被调用。
setContentView(R.layout.activity_main);
TextView hpT=(TextView)findViewById(R.id.textView3);
TextView hpA=(TextView)findViewById(R.id.textView4);
TextView mw=(TextView)findViewById(R.id.mw);