[android]值存储在首选项中共享

发布时间: 2016/9/25 23:00:01
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

你好,我想要拯救我的高分数通过共享首选项的值。但当我去到另一个 activity ,然后进入相同 activity 我高得分值再次从零开始。我知道它的简单,但我不知道我做的错了甚么。下面是示例代码。

    private static final String FORMAT = "%02d:%02d:%02d";
    int count = 0, high;
    int seconds , minutes;
    SharedPreferences sharedPreferences;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        sharedPreferences = getPreferences(MODE_PRIVATE);
        int savedPref = sharedPreferences.getInt("HighScore", 0);

        final TextView counter=(TextView)findViewById(R.id.taptimes);
        final TextView countdown=(TextView)findViewById(R.id.countdown);
        final TextView highScore = (TextView)findViewById(R.id.highScore);
        final Button b1=(Button)findViewById(R.id.keypress);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                countClick();
            }

            private void countClick() {
                // TODO Auto-generated method stub
                count++;
                counter.setText(String.valueOf(count));
                if (count > high){

                    highScore.setText(Integer.toString(count));
                    high = count;

                    sharedPreferences = getPreferences(MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("HighScore", high);
                    editor.commit();
                }
            }
        });


        new CountDownTimer(10000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long

             public void onTick(long millisUntilFinished) {
                 countdown.setText("Time remaining: " + millisUntilFinished / 1000);
              //here you can have your logic to set text to edittext
             }

             public void onFinish() {
                 countdown.setText("Time Over!");
                 b1.setEnabled(false);
             }
            }
            .start();

    }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, 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);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }


}

解决方法 1:

通过使用 OnCreate 方法我只是错过了这些。谢谢大家,

high = sharedPreferences.getInt("HighScore", 0);
 highScore.setText(Integer.toString(high));
官方微信
官方QQ群
31647020