用于完成点击次数计数的共享首选项的语法

时间:2018-06-14 18:00:37

标签: java android

您好我需要一点帮助来完成我拥有的共享首选项代码。

目前我有一个类,我存储在SharedPreferences的相关代码中:

public class SharedPreferencesManager {

    private static final String APP_PREFS = "AppPrefsFile";
    private static final String NUMBER_OF_CLICKS = "numberOfClicks";

    private SharedPreferences sharedPrefs;
    private static SharedPreferencesManager instance;



    private SharedPreferencesManager(Context context) {
        sharedPrefs =
                context.getApplicationContext().getSharedPreferences(APP_PREFS, MODE_PRIVATE);
    }


    public static synchronized SharedPreferencesManager getInstance(Context context){
        if(instance == null)
            instance = new SharedPreferencesManager(context);

        return instance;
    }

    public void storeClicks(int count)
    {
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putInt(NUMBER_OF_CLICKS, count);
        editor.apply();
    }

    public int getNumberOfClicks(){
        int clicksNumber = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
        return clicksNumber;
    }
}

我想计算来自MainActivity类的按钮笑话,诗歌和搞笑故事的点击次数,并选择另一个' Content类中的按钮。因此,如果单击每个按钮4次,则总点击次数应为16。

即使应用关闭并重新打开,我也想保持点击次数。它只是我不确定的点击计数的语法。我认为主要活动是正确的,但我不确定如何将它存储到Content类。

总结一下,计算所有这些按钮的点击次数,无论用户在哪个页面,都应该可以访问和更新此号码。

以下是主要活动类:

public class MainActivity extends AppCompatActivity{

    final Context context = this;

    int clicksCount = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferencesManager.getInstance(context).storeClicks(clicksCount);


        Button jokesButton = findViewById(R.id.button_jokes);
        Button poemsButton = findViewById(R.id.button_poems);
        Button funnyStoriesButton = findViewById(R.id.button_funny_stories);


        jokesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                clicksCount++;
            }
        });

        poemsButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                clicksCount++;
            }
        });

        funnyStoriesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                clicksCount++;
            }
        });


}

以下是内容类:

public class Content extends AppCompatActivity{

    Button backButton;
    Button selectAnotherButton;
    TextView contentText;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

        int clicksCount = SharedPreferencesManager.getInstance(context).getNumberOfClicks();

        backButton = findViewById(R.id.button_back);
         selectAnotherButton = findViewById(R.id.button_select_another);

        selectAnotherButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    clicksCount++;

                }
        });

            backButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick (View v){
                finish();
            }
        });
    }




}

1 个答案:

答案 0 :(得分:2)

您的getNumberOfClicks()中没有storeClicks()SharedPreferencesManager这两种方法,您只能拥有这样的方法:

public void increaseClickCount() {
    int clickCount = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
    clickCount++;
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.putInt(NUMBER_OF_CLICKS, clickCount);
    editor.apply();
}

每次点击都会调用此功能。不需要活动就知道点击次数。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final SharedPreferencesManager prefManager = SharedPreferencesManager.getInstance(context);    

    jokesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            prefManager.increaseClickCount();
        }
    });

    ...
}

此外,您无需将上下文存储为全局变量,因为您的活动本身已经是上下文,您可以使用thisMainActivity.this作为上下文。