gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:27:21 25 4
gpt4 key购买 nike

大家好,我的代码有问题,如果我将 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。您的代码无法使 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)

最佳答案

实际上我不太明白你想要做什么,但错误是你从不切换你的 boolean 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;
}

关于java - 如果将 SharedPreferences 放在那里,代码将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31983038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com