按钮未在其他活动中禁用

时间:2017-01-22 17:20:57

标签: java android button android-button

我是android dev的初学者,我正在创建一个披萨点击游戏,就像cookie点击器一样。我创建了升级和升级活动,你需要一些披萨,比如你有10个比萨,你可以升级。如果比萨饼的数量等于按钮启用的价格,如果没有,则不启用该按钮。当我点击按钮时,披萨的数量会减少,按钮应该再次禁用,但它不会禁用。

这是第一项活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

public static int pizza = 0;
public static TextView pizzaContText, helpers;
public static Button add, upgrades, exit;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
}

private void initialize() {
    add = (Button) findViewById(R.id.makePizza);
    exit = (Button) findViewById(R.id.exitButton);
    upgrades = (Button) findViewById(R.id.upgrades);

    pizzaContText = (TextView) findViewById(R.id.pizzas);
    helpers = (TextView) findViewById(R.id.helpers);

    pizzaContText.setText("Pizzas: " + pizza);
    pizzaContText.setTextColor(Color.BLACK);
    pizzaContText.setTextSize(40);

    helpers.setText("Helpers: " + Upgrades.contHelper);
    helpers.setTextSize(20);
    helpers.setTextColor(Color.BLACK);

    add.setOnClickListener(this);
    upgrades.setOnClickListener(this);
    exit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.makePizza:
            pizza++;
            pizzaContText.setText("Pizzas: " + pizza);
            pizzaContText.setTextColor(Color.BLACK);
            pizzaContText.setTextSize(40);
            break;

        case R.id.upgrades:
            Intent i = new Intent(getApplicationContext(), Upgrades.class);
            startActivity(i);
            break;

        case R.id.exitButton:
            finish();
            System.exit(0);
            break;
    }
}

}

这是第二项活动(升级):

public class Upgrades extends AppCompatActivity implements View.OnClickListener{

public static int contHelper = 0, priceHelper = 10;
Button addHelper, back;
Handler h = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upgrades);
    initialize();


    if (MainActivity.pizza >= priceHelper){
        //ENABLES THE BUTTON
        addHelper.setEnabled(true);
    } else{
        //DISABLE THE BUTTON
        addHelper.setEnabled(false);
    }
}

private void initialize() {
    addHelper = (Button) findViewById(R.id.addHelper);
    addHelper.setText("Helper: " + priceHelper + " pizzas");
    back = (Button) findViewById(R.id.back);

    addHelper.setOnClickListener(this);
    back.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.addHelper:
            MainActivity.pizza-=priceHelper;
            addHelper.setText("Helper: " + priceHelper + " pizzas");
            priceHelper+=4;
            contHelper++;

            //Auto clicks the make pizza button every 1 sec
            final Runnable r = new Runnable() {
                @Override
                public void run() {
                    MainActivity.add.performClick();
                    h.postDelayed(this, 1000);
                }
            };
            h.postDelayed(r, 1000);
            break;

        case R.id.back:
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
            break;
    }
}

}

1 个答案:

答案 0 :(得分:0)

不要使用静态对象..这是你能做的最糟糕的事情..使用bundle发送你的变量

Intent activity = new Intent(this, Upgrades.class);
activity.putExtra("pizza", pizza);
startActivity(intent);

在升级活动中使用

Bundle extras = getIntent().getExtras();
int pizza = extras.getInt("pizza");

检查空值并发送正确的内容。