java - 我有一个按钮没有反应。当我关闭屏幕并重新打开它后,它会做出响应。其他按钮工作正常

标签 java android

这是我的主要内容

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 从 7 开始,当我击中时,它没有响应。但是当我关闭屏幕并再次打开它后,它有响应。
我也将所有变量放在不同的类中。第 1 回合应该发牌,第 2 回合开始时显示牌。在 dealing.deal 中,它将回合设置为回合 2。它可以工作,但您必须关闭屏幕并将其打开才能继续。

最佳答案

约翰,

您当前的代码只会运行一次,因为该方法在应用程序的整个生命周期 onCreate 中仅被调用一次。您需要执行以下操作:

  1. 在 XML 中定义按钮的位置添加以下代码:

    android: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. 重新格式化您的代码以使其更具可读性 - 将不同的代码委托(delegate)给不同的函数!!

编辑 仅使用以下代码替换您创建的方法中的所有内容

turn = 1;

关于java - 我有一个按钮没有反应。当我关闭屏幕并重新打开它后,它会做出响应。其他按钮工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690236/

相关文章:

java - 在android中创建应用程序 session

android - 无法在 Android 中滚动 webview

java - 我有一个 mysql 数据库和一个表,我将它编码为 json,当我在浏览器中运行 php 文件时,当我从 android 获取它时,它会有所不同

android - 程序类型已存在 : com. google.android.gms.internal.zzak

android - 如何在android中获取当前位置的经纬度

android - 在 CameraPreview 上应用 EffectFactory 效果

java - 如何表达不在M :N relation with JPA 2. 0 Criteria中?

java - ant 脚本中面向 future 的 java 版本检查

Java 抽象和接口(interface)

java - java中查找字符串中的重复项