简体   繁体   English

我有一个没有反应的按钮。 在我关闭屏幕并将其重新打开后,它会响应。 其他按钮工作正常

[英]I have a button that doesnt respond. After I turn off screen and turn it back on it responds. Other button works fine

this is my main这是我的主要

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();
        }

    }
}

Turn starts out at 7 and when I hit, it it doesn't respond.转弯从 7 点开始,当我击中时,它没有响应。 But after I close the screen and open it again, it responds.但是在我关闭屏幕并再次打开它后,它会响应。
Also I have all the variables in a different class.此外,我在不同的类中拥有所有变量。 Turn 1 is supposed to deal the cards and at the start of turn 2 it displays the cards.第 1 回合应该发牌,而在第 2 回合开始时,它会显示卡片。 In the dealing.deal it sets turn to turn 2. It works but you have to close the screen and turn it on for it to proceed.dealing.deal它设置为 turn 2。它可以工作,但您必须关闭屏幕并打开它才能继续。

John,约翰,

Your current code will only run once, as the method is only called once in the whole lifecycle of the app, onCreate.您当前的代码只会运行一次,因为该方法在应用程序的整个生命周期 onCreate 中只会被调用一次 What you need to do is the following:您需要做的是:

  1. Where you've defined your button in XML, add this code:在 XML 中定义按钮的位置,添加以下代码:

    android:onClick="YOUR_METHOD_NAME" android:onClick="YOUR_METHOD_NAME"

  2. Create the method in the class with the following syntax:使用以下语法在类中创建方法:

     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. Reformat your code to make it more readable - delegate different code to different functions!!重新格式化您的代码以使其更具可读性 - 将不同的代码委托给不同的功能!!

EDIT replace everything in the method you created with only the following code编辑仅使用以下代码替换您创建的方法中的所有内容

turn = 1;

try this试试这个

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: there is a better way to do this , create a method with a View argument like : ps:有一个更好的方法来做到这一点,创建一个带有 View 参数的方法,如:

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

now on your layout.xml here you have the Button start现在在你的 layout.xml 上你有按钮开始

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

this way you do NOT need create a object of your Button in your java code, and it looks nice too.这样你就不需要在你的 java 代码中创建你的 Button 对象,它看起来也不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM