2016-08-01 21 views
0

這是我的主要我有一個不響應的按鈕。關閉屏幕並重新打開後,屏幕響應。其他按鈕正常工作。幫助請

package com.example.student.poker; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 

import static com.example.student.poker.variables.*; 

public class MainActivity extends AppCompatActivity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     setContentView(R.layout.activity_main); 
     playeronestack = (TextView) findViewById(R.id.playeronechip); 
     playertwostack = (TextView) findViewById(R.id.playertwochip); 
     playeronecheckbutton = (Button) findViewById(R.id.playeronecheck); 
     playertwocheckbutton = (Button) findViewById(R.id.playertwocheck); 
     playeronebetbutton = (Button) findViewById(R.id.playeronebet); 
     playertwobetbutton = (Button) findViewById(R.id.playertwobet); 
     playeronefoldbutton = (Button) findViewById(R.id.playeronefold); 
     playertwobutton = (Button) findViewById(R.id.playertwofold); 
     playeronebettext = (EditText) findViewById(R.id.playeronespecifybet); 
     playertwobettext = (EditText) findViewById(R.id.playertwospecifybet); 
     playeronefirstcard = (ImageView) findViewById(R.id.playeronefirst); 
     playertwofirstcard = (ImageView) findViewById(R.id.playertwofirst); 
     playeronesecondcard = (ImageView) findViewById(R.id.playeroneSecond); 
     playertwosecondcard = (ImageView) findViewById(R.id.playertwosecond); 
     firstflopcard = (ImageView) findViewById(R.id.firstflop); 
     secondflopcard = (ImageView) findViewById(R.id.secondflop); 
     thirdflopcard = (ImageView) findViewById(R.id.thirdflop); 
     fourthflopcard = (ImageView) findViewById(R.id.fourthflop); 
     fifthflopcard = (ImageView) findViewById(R.id.fifthflop); 
     dealerbuttonone = (ImageView) findViewById(R.id.dealerbuttonone); 
     dealerbuttontwo = (ImageView) findViewById(R.id.dealerbuttonone); 
     pottext = (TextView) findViewById(R.id.potsize); 
     startbutton = (TextView) findViewById(R.id.startbutton); 


     if(turn == 2){ 
      preflop.preflopturn(); 
     } 
     if(turn == 7){ 
      startbutton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        turn = 1; 
       } 
      }); 
     } 
     if(turn == 1) { 
      dealing.deal(); 
     } 






    } 
} 

我知道它的馬虎我剛剛開始;) 又將在7開始了,當我打它它不響應。但是在關閉屏幕並再次打開後,它會響應。它很奇怪; - ;.任何幫助? 另外我有一個不同類的所有變量。第一回合應該處理牌,第二回合開始時顯示牌。在deal.deal。它設置匝2.它的工作原理,但你必須關閉屏幕並打開它進行

+0

其中ID聲明轉彎,什麼是默認值? –

+0

我用我所有的變量在不同的java類中聲明它。它開始於7 –

+0

btw我有2個XML文件,它們是activity_main.xml和activity_main.xml(陸地)。不確定它使用的是什麼; - ; –

回答

0

約翰,

您當前的代碼將只運行一次,作爲應用程序,的onCreate的整個生命週期的方法只調用一次。你需要做的是以下幾點:

  1. 如果你在XML定義你的按鈕,添加以下代碼:

    安卓的onClick = 「YOUR_METHOD_NAME」

  2. 創建方法以下語法類:

    public void YOUR_METHOD_NAME (View v) { 
    if (turn == 1) { 
    
    } 
    else if (turn == 2) { 
    
    } 
    else if (turn == 7) { 
    
    } 
    /*Add a fallback clause in case none of the  conditions are met as a safeguard*/ 
    } 
    
  3. 重新格式化您的代碼,使其更具可讀性 - 委託不同的代碼不同的功能!

編輯 替換你只用下面的代碼創建的方法都

turn = 1; 
0

試試這個

startbutton.setOnClickListener(new View.OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     if(turn == 2) preflop.preflopturn(); 
     if(turn == 7) turn = 1; 
     if(turn == 1) dealing.deal(); 
    } 
}); 

PS:有一個更好的方式來做到這一點,創建一個法像一個View參數:

 public void buttonStart(View v) 
    { 
     // place your if , else and anything else here 
    } 
現在

您layout.xml在這裏你有按鈕開始

<Button 
     android:id="@+id/startbutton" 
     ...... 
     ...... 
     aandroid:onClick="buttonStart"> 

這樣你不需要在你的java代碼中創建你的Button的對象,而且它看起來也不錯。

+0

startbutton.setOnClickListener(新View.OnClickListener() { @Override 公共無效的onClick(視圖v) { 如果(轉== 2)preflop.preflopturn();如果 (導== 7)反過來= 1; if(turn == 1)dealing.deal(); } }); –

+0

我想讓開始按鈕只啓動遊戲。我不希望它能夠推動遊戲向前發展。我遇到的唯一問題是startbutton的工作原理,但是隻有在關掉手機後才能開始。關閉屏幕的按鈕 –

+0

感謝您使用if語句創建按鈕的提示tho :) –