2016-08-01 23 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 :) –