在我的应用中存储数据(高分)

时间:2016-11-17 18:38:43

标签: javascript android sharedpreferences datastore

我开发了一个点击程序,当我点击一个按钮然后分层计算点击次数和15秒后按钮鹅禁用,我想要**我在15秒内点击了多少存储作为高分,当我越过那个高得分然后将我的新高分节目存储在相同的活动**

2 个答案:

答案 0 :(得分:1)

您可以使用共享偏好保存并检查您的高分。像这样:

这些行在开头的set_ContentView下:

String PREFS_GAME ="your package name";
SharedPreferences sp = getSharedPreferences(PREFS_GAME,Context.MODE_PRIVATE);
final Integer oldrec  = sp.getInt("record",0);

然后在setOnclicklistenre中编写此代码:

if (newrec>oldrec){
                sp.edit().putInt("record",new rec).commit();
                Toast.makeText(MainActivity.this,"your new record is :"+newrec, Toast.LENGTH_SHORT).show();
                }

答案 1 :(得分:0)

我建议:

public class MainActivity extends AppCompatActivity {

private int clicks = 0;
private TextView mTextView;
Button bt_restart;
int high;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String HIGHSCORE = "high" ;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); 
    high = prefs.getInt(HIGHSCORE, 0); 
    bt_restart = (Button)findViewById(R.id.restart);
    bt_restart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent restartIntent = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(getBaseContext().getPackageName());
            restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(restartIntent);
        }
    });

    mTextView = (TextView) findViewById(R.id.total_textview);
    mTextView.setVisibility(View.VISIBLE);

    Button button = (Button) findViewById(R.id.count_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            final Button b = (Button)v;
            if (clicks == 0){
                // Means its the first time that a user click the button
                // Start a thread that is going to disable the button after 5 seconds from first click
                new CountDownTimer(15000, 1000) {
                    public void onTick(long millisUntilFinished) {
                        b.setText(millisUntilFinished / 1000 + " Seconds");
                    }

                    public void onFinish() {
                        b.setText("Time up");
                        b.setEnabled(false);
                        // Showing user clicks after button is disabled
                        showClicks();
                    }
                }.start();
            }
            // Here we are just counting  . . . . including the first click
            countClicks();
        }
    });

}


private void countClicks(){
    ++clicks;
    mTextView.setText(Integer.toString(clicks));

    // You can update your text view here
}
private void showClicks(){
    mTextView.setText(String.valueOf(clicks)+"Clicks");
    mTextView.setVisibility(View.VISIBLE);
    if(clicks > high){
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
        editor.putInt(HIGHSCORE, clicks);
        editor.commit();
        high = prefs.getInt(HIGHSCORE, 0); 
    }    
}

然后如果你想要显示高分数使用textview并且在setText中使用String.valueOf(高),我不知道当改变变量高时textview是否会改变你可能必须使用textview.setText everyTima您编辑变量high(基于该链接的代码:Android Shared preferences example

相关问题