2015-10-31 80 views
0

我想要做的是禁用這些按鈕如果Boolean num10,num11等等設置爲true。但是當我打開SecondActivity時,它總是顯示禁用的按鈕。即使我沒有按下按鈕。按鈕始終設置爲禁用

我該怎麼辦?

這是我的代碼:

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class SecondActivity extends AppCompatActivity { 

    Button buyItem1,buyItem2,buyItem3,buyItem4; 
    SharedPreferences mySharedPreferences2; 
    boolean num10 = false, num11 = false, num12 = false, num13 = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     mySharedPreferences2 = PreferenceManager.getDefaultSharedPreferences(this); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     Intent intent = getIntent(); 
     final int goldamountval = intent.getIntExtra("parameter1", 0); 
     TextView goldamount = (TextView) findViewById(R.id.goldamount); 
     goldamount.setText("" + goldamountval); 
     buyItem1 = (Button) findViewById(R.id.buyitem1); 
     buyItem2 = (Button) findViewById(R.id.buyitem2); 
     buyItem3 = (Button) findViewById(R.id.buyitem3); 
     buyItem4 = (Button) findViewById(R.id.buyitem4); 
     buyItem1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       if(goldamountval >= 1000){ 
        SharedPreferences.Editor editor = mySharedPreferences2.edit(); 
        editor.putBoolean(String.valueOf(num10), true); 
        editor.apply(); 
       } 
      } 
     }); 

     buyItem2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       if(goldamountval >= 5000){ 
        SharedPreferences.Editor editor = mySharedPreferences2.edit(); 
        editor.putBoolean(String.valueOf(num11), true); 
        editor.apply(); 
       } 
      } 
     }); 

     buyItem3.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       if(goldamountval >= 10000){ 
        SharedPreferences.Editor editor = mySharedPreferences2.edit(); 
        editor.putBoolean(String.valueOf(num12), true); 
        editor.apply(); 
       } 
      } 
     }); 

     buyItem4.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       if(goldamountval >= 50000){ 
        SharedPreferences.Editor editor = mySharedPreferences2.edit(); 
        editor.putBoolean(String.valueOf(num13), true); 
        editor.apply(); 
       } 
      } 
     }); 
     if (num10 = true){ 
      buyItem1.setEnabled(false); 
      Button button = (Button)findViewById(R.id.item4); 
      button.setText("Drill"); 
     } 
     if (num11 = true){ 
      buyItem2.setEnabled(false); 
      Button button = (Button)findViewById(R.id.item5); 
      button.setText("Excavator"); 
     } 
     if (num12 = true){ 
      buyItem3.setEnabled(false); 
      Button button = (Button)findViewById(R.id.item6); 
      button.setText("Digger"); 
     } 
     if (num13 = true){ 
      buyItem4.setEnabled(false); 
      Button button = (Button)findViewById(R.id.item7); 
      button.setText("Mine Permission"); 
     } 
    } 
} 

回答

2

if分配布爾(if (num10 = true)),所以他們總是true比每個按鈕被禁用。你需要改變這樣的代碼:

if (num10 == true){ 
    buyItem1.setEnabled(false); 
    Button button = (Button)findViewById(R.id.item4); 
    button.setText("Drill"); 
} 
if (num11 == true){ 
    buyItem2.setEnabled(false); 
    Button button = (Button)findViewById(R.id.item5); 
    button.setText("Excavator"); 
} 
if (num12 == true){ 
    buyItem3.setEnabled(false); 
    Button button = (Button)findViewById(R.id.item6); 
    button.setText("Digger"); 
} 
if (num13 == true){ 
    buyItem4.setEnabled(false); 
    Button button = (Button)findViewById(R.id.item7); 
    button.setText("Mine Permission"); 
} 
+0

:D我忘記了,謝謝! –

+0

只能說'if(num10)' –

相關問題