我第一次完成没有首选项的编码时按钮可以看到但是当我把偏好代码放在按钮上时不可见
我有3个类,它是menu.class,levelone.class,leveltwo.class
这是menu.class
的以下代码
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
我已经放了这段代码
public void onClick(View v){
setResult (2);
finish();
}
});
该代码是在menu.class
中显示按钮,但levelone.class
完成时();按钮没有任何意义,它仍然是GONE
f2按钮功能是打开leveltwo.class
,而leveltwo.class
中有相同的代码来设置f3按钮可见
public void onClick(View v){
setResult (3);
finish();
}
});
等等,下一个级别有相同的代码使按钮可见 我的代码setResult是错误的还是偏好代码使它不起作用?
答案 0 :(得分:0)
直线:
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();
你只是将“false”放在" f2",因为levelTwoUnlocked始终为false。
答案 1 :(得分:0)
为leveTwoUnloacked
分配一个布尔值
要么
editor.putBoolean("f2",true);
并检查
if(prefernces.getBoolean("f2",false))
{
//your code
}