2014-11-08 3 views
2

저는 안드로이드 프로그래밍에 새로운 경험이 있습니다. 나는 3 개의 "공격"버튼으로 간단한 애플리케이션을 만들고 싶다. 사용자가 버튼 하나를 누르면, 플레이어는 손상을 계산하고, 플레이어의 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

** igralec과 관련이 있습니다 ** if 조건을 전달하는 중 ... onClick()이 매번 호출됩니다. – nobalG

답변

1

onClick()에서

setContentView(R.layout.activity_main); 

를 제거합니다.

동일한 레이아웃을 다시 팽창시키고 새로운 레이아웃 인스턴스에 onclick 리스너가 설치되어 있지 않습니다.

0

onClick(View v)에는 이미 Activity에 있던 모든 버튼을 onClickListeners과 대체하는 setContentView(R.layout.activity_main);을 호출합니다.

이처럼 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에서 한 번 확인할 수 있습니다.

관련 문제