共享preferences code无功能,使按钮可见 [英] The SharedPreferences code was no function to make button Visible

查看:142
本文介绍了共享preferences code无功能,使按钮可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次做了没有$ P $编码pferences的按钮就可以看到,但是当我把preferences code中的按钮不可见。

First time i had done coding without preferences the button can visible but when i put preferences code the button not visible

我有3类是menu.class,levelone.class,leveltwo.class

i have 3 class it is menu.class, levelone.class, leveltwo.class

这是 menu.class 以下code这样的

this is the following code of menu.class like this

public class menu extends Activity {

Button f1, f2;
ImageView f2lock;
boolean levelTwoUnlocked;


@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:   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);
        }      
      }
    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);
}

}

levelone.class 我已经把这个code

and in levelone.class i have put this code

 public void onClick(View v){
                  setResult (2);
                  finish();          
                  }
                }); 

这就是code是使按钮可见 menu.class 但是当 levelone.class 完成( );在没有发生与按钮,它仍然GONE

thats code is to make button visible in menu.class but when levelone.class finish(); is nothing happen with the button, it's still GONE

F2按键的功能是打开 leveltwo.class leveltwo.class 有相同的code到设置F3按钮可见

f2 button function is to open leveltwo.class and in leveltwo.class had same code to set f3 button visible

public void onClick(View v){
                      setResult (3);
                      finish();          
                      }
                    }); 

等都与下一级有一个相同的code键使按键可见
难道我的code的setResult是错误或preferences code,使其无法正常工作?

and so on with the next level had a same code to make button visible Did my code setResult is wrong or the preferences code make it not function?

推荐答案

一个布尔值分配给 leveTwoUnloacked
 要么
做出改变。

Assign a boolean value to leveTwoUnloacked or make changes in

editor.putBoolean(F2,真正的);

和检查

if(prefernces.getBoolean("f2",false))
{
//your code
}

这篇关于共享preferences code无功能,使按钮可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆