如果SharedPreferences放在那里,代码不起作用

时间:2015-08-13 08:21:09

标签: java android boolean sharedpreferences

Hy家伙我的代码有问题,如果我把SharedPreferences我的代码不起作用

我将用以下代码解释

这是menu.class

public class menu extends Activity {

    Button f1, f2;
    ImageView f2lock;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.famouslevel);
        f1 =(Button)findViewById(R.id.f1);      

        f1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                Intent level1 = new Intent ();
                level1.setClassName ("com.example.game", "com.example.game.levelone");
                startActivityForResult (level1, 0);              
            }             
        });     
    }   

    public void onActivityResult (int requestCode, int resultCode, Intent level1){
        super.onActivityResult (requestCode, resultCode, level1); 
        f2=(Button)findViewById(R.id.f2);      
        f2lock=(ImageView)findViewById(R.id.f2lock);

        switch (resultCode) {
            case 2:  f2.setVisibility(View.VISIBLE);
                     f2lock.setVisibility(View.GONE);            
        }      

        f2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                Intent level2 = new Intent ();
                level2.setClassName ("com.example.game", "com.example.game.leveltwo");
                startActivityForResult (level2, 0);              
            }             
        });       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splashscreen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

如果我使用此代码

        switch (resultCode) {
        case 2:  f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);            
        }   

代码运行正常,menu.xml中的f2按钮显示为VISIBLE和f2lock GONE,但当然没有SharedPreferences它不会保存

因此,如果我更改代码并将SharedPreferences放在这样:

switch (resultCode) {
    case 2:    
        SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);            
        SharedPreferences.Editor editor = preferences.edit(); 
        editor.putBoolean("f2", levelTwoUnlocked);
        editor.commit();

        if(levelTwoUnlocked){
            f2.setVisibility(View.VISIBLE);
            f2lock.setVisibility(View.GONE);            
        }
        else {
            f2.setVisibility(View.GONE);
            f2lock.setVisibility(View.VISIBLE);
        }      
}

menu.xml中的f2按钮未转VISIBLE,仍然是GONE。 Yhe代码不能用于制作f2按钮VISIBLE和f2lock GONE

任何人都可以帮我这个代码吗?

已更新

我再次更改了我的代码

switch (resultCode) {
        case 2:    
            SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE); 
            //to make f2 VISIBLE and f2lock GONE
            boolean levelTwoUnlocked = preferences.getBoolean("f2", true);      
            SharedPreferences.Editor editor = preferences.edit(); 
            editor.putBoolean("f2", levelTwoUnlocked);
            editor.commit();

            if(levelTwoUnlocked){
                f2.setVisibility(View.VISIBLE);
                f2lock.setVisibility(View.GONE);            
            }
            else {
                f2.setVisibility(View.GONE);
                f2lock.setVisibility(View.VISIBLE);
            }      
    }

仍然有同样的问题,f2赢得了setVisibility(View.VISIBLE)

1 个答案:

答案 0 :(得分:2)

实际上我并不清楚你正在尝试做什么,但错误的是你永远不会切换你的布尔levelTwoUnlocked。所以不记得你在if中输入的次数,路由将始终相同(我打赌levelTwoUnlocked=false因为是默认的Java boolean值):

if(levelTwoUnlocked){
    f2.setVisibility(View.VISIBLE);
    f2lock.setVisibility(View.GONE);            
    levelTwoUnlocked = false;
} else {
    f2.setVisibility(View.GONE);
    f2lock.setVisibility(View.VISIBLE);
    levelTwoUnlocked = true;
}