1

*过去几天我一直在为这个问题发疯。我正在尝试使用 SharedPreferences 来允许我的用户通过 ToggleButton 保存游戏中音频的选定选项,但每次我运行我的应用程序并点击我的“完成”按钮返回到 main_activity 屏幕,然后再次单击设置,它永远不会保存。我已经通过谷歌搜索对此做了一些研究,我的书和这个网站,但通过很多方法,我尝试过结果是一样的. 我是android开发的新手,更不用说应用程序开发了关于我在这里做错了什么。这是我的设置活动中的代码。

package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;    

public class Settings extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);  

        Button Notifications = (Button) findViewById(R.id.Notifications);
        Button Done = (Button) findViewById(R.id.done);
        Button AccountSettings = (Button) findViewById(R.id.AccountSettings);
        final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle);
        AT.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                {
                    if ((AT.isChecked()))
                    {
                        SharedPreferences appPrefs =
                        getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
                                        MODE_PRIVATE);
                        SharedPreferences.Editor editor = appPrefs.edit();
                        editor.putBoolean("atpref", AT.isChecked()); //value to store
                        editor.commit();
                    }
                }
            }
        });
        SharedPreferences appPrefs =
                getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
                MODE_PRIVATE);
        boolean atpref = appPrefs.getBoolean("atpref", true); //default is true
        AT.setChecked(atpref);
        Done.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent Intent = new Intent(Settings.this,activity_main.class);
                Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(Intent);



            }
        });
        Notifications.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications"));
            }
        });
        AccountSettings.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings"));
            }
        });

    }    

    public void onToggleClicked(View view) {
        // Is the toggle on?
        boolean on = ((ToggleButton) view).isChecked();

        if (on) {
            view.setSoundEffectsEnabled(true);
        } else {
            view.setSoundEffectsEnabled(false);

        }
    }

}
4

1 回答 1

2

如果您尝试保存 ToggleButton 的状态false,只需删除 OnClickListener 中的 if 语句:

if ((AT.isChecked()))

于 2013-03-23T22:36:11.523 回答