【问题标题】:Button is not disabling in another activity按钮未在另一个活动中禁用
【发布时间】:2017-01-22 17:20:56
【问题描述】:

我是 android 开发的初学者,我正在创建一个披萨点击游戏,就像 cookie clicker 一样。我创建了一个升级活动,升级你需要一些披萨,比如如果你有 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;
    }
}

}

【问题讨论】:

  • 哪行代码禁用了你的按钮,请识别

标签: java android button android-button


【解决方案1】:

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

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

并在您的升级活动中使用

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

并检查空值以及您发送的内容是否正确。

【讨论】:

  • 我按照你说的做了,但是当我尝试进行升级活动时应用程序停止工作
  • 没关系。 Android 生命周期可能因手机而异。静态是最糟糕的事情。但随心所欲..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
  • 2012-02-12
相关资源
最近更新 更多