1

我得到了这个跟踪器屏幕,它可以跟踪用户的锻炼天数和完成的锻炼集。我想通过增加我的计数器来添加ProgressBarRatingBar显示某种进度/评级。

假设ProgressBar当 counter2 达到 300(counter2(锻炼组已完成))时,将获得 5 星或满星。

在此处输入图像描述

我当前的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    a = new AlphaAnimation(1, 0);
    a.setDuration(120);

    if (savedInstanceState == null) {
        counter1 = 0;
        counter2 = 0;
    }
    setContentView(R.layout.tracker);

    counter1 = 0;
    counter2 = 0;

    add1 = (Button) findViewById(R.id.baddday);
    sub1 = (Button) findViewById(R.id.bsubday);
    add2 = (Button) findViewById(R.id.baddset);
    sub2 = (Button) findViewById(R.id.bsubset);
    list = (Button) findViewById(R.id.blist);
    bar = (ProgressBar) findViewById(R.id.progressBar1);

    display1 = (TextView) findViewById(R.id.tvDisplayDays);
    display2 = (TextView) findViewById(R.id.tvDisplaySets);
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/tele.ttf");
    display1.setTypeface(tf);
    display2.setTypeface(tf);

    add1.setOnClickListener(this);
    sub1.setOnClickListener(this);
    add2.setOnClickListener(this);
    sub2.setOnClickListener(this);
    list.setOnClickListener(this);

    SharedPreferences setting = getSharedPreferences("countersetting", 0);
    counter1 = setting.getInt("countervalue1", 0);
    counter2 = setting.getInt("countervalue2", 0);
    display1.setText("" + counter1);
    display2.setText("" + counter2);
            bar.setProgress(counter2);
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString("YourTextViewTextIdentifier1", display1
            .getText().toString());
    savedInstanceState.putString("YourTextViewTextIdentifier2", display2
            .getText().toString());
    savedInstanceState.putInt("int", counter1);
    savedInstanceState.putInt("int", counter2);
    super.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    display1.setText(savedInstanceState
            .getString("YourTextViewTextIdentifier1"));
    display2.setText(savedInstanceState
            .getString("YourTextViewTextIdentifier2"));
    counter1 = savedInstanceState.getInt("int");
    counter2 = savedInstanceState.getInt("int");
}

@Override
protected void onStop() {
    super.onStop();

    SharedPreferences setting = getSharedPreferences("countersetting", 0);
    SharedPreferences.Editor editor = setting.edit();
    editor.putInt("countervalue1", counter1);
    editor.putInt("countervalue2", counter2);
    editor.commit();
}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    switch (view.getId()) {

    case R.id.baddday:
        counter1 += 1;
        display1.setText("" + counter1);
        add1.startAnimation(a);
        break;
    case R.id.bsubday:
        counter1 -= 1;
        display1.setText("" + counter1);
        sub1.startAnimation(a);
        break;
    case R.id.baddset:
        counter2 += 1;
        display2.setText("" + counter2);
        add2.startAnimation(a);

        bar.setProgress(counter2);
        bar.setMax(300);
        bar.incrementProgressBy(counter2);
        bar.getProgress();



        break;
    case R.id.bsubset:
        counter2 -= 1;
        display2.setText("" + counter2);
        sub2.startAnimation(a);
        bar.setProgress(counter2);
        bar.incrementProgressBy(counter2);
        bar.getProgress();

        break;
    case R.id.blist:
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                Intent i = new Intent(Tracker.this, List.class);
                startActivity(i);
            }
        }, 150);
        list.startAnimation(a);
        break;

    }

}

}

4

1 回答 1

0

假设计数器 2 达到 300 时(计数器 2(锻炼组完成))将获得 5 颗星或完整的进度条

您已经为 设置了正确的进度,ProgressBar但是您还再次增加了新设置的进度(按计数器值),这不会显示正确的结果。在onCreate方法中:

bar.setMax(300);
bar.setProgress(counter2); // the initial stored value

并在按钮的侦听器中:

case R.id.baddset:
    counter2 += 1;
    display2.setText(String.valueOf(counter2));
    add2.startAnimation(a);
    bar.setProgress(counter2);
    break;
case R.id.bsubset:
    counter2 -= 1;
    display2.setText(String.valueOf(counter2));
    sub2.startAnimation(a);
    bar.setProgress(counter2);
    break;
于 2013-03-19T13:10:29.183 回答