2014-11-08 72 views
2

所以,我對android編程很陌生。我想用三個「攻擊」按鈕做一個簡單的應用程序,當用戶按下一個按鈕時,它應該計算傷害,改變玩家的一個hp值並輸出一些文字(誰傷害了多少人)。一切正常,但只是第一次。在第一個按鈕按下後,應用程序不再響應按鈕按下。Android onClickListener只能工作一次

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); 
    } 
} 
+0

它與您在if條件中傳遞的** igralec **有關...... onClick()每次都會被調用。 – nobalG 2014-11-08 14:17:57

回答

1

onClick()取出

setContentView(R.layout.activity_main); 

您正在重新填充相同的佈局,並且新佈局實例沒有安裝onclick偵聽器。

0

在您的onClick(View v)中,您可以撥打setContentView(R.layout.activity_main);,它將替換Activity中已包含的所有按鈕及其onClickListeners

重寫你的onCreate方法是這樣的(和聲明字段變量):

private TextView hpT, hpA, mw; 

@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); 

    hpT = (TextView)findViewById(R.id.textView3); 
    hpA = (TextView)findViewById(R.id.textView4); 
    mw = (TextView)findViewById(R.id.mw); 
} 

,並從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通話非常耗時,找到的View在點擊之間不會發生變化 - 您可以在onCreate中找到一次。