设置SeekBar值

时间:2015-11-02 09:31:42

标签: android seekbar

我正在使用SeekBar和两个TextView创建一个简单的高度计算应用程序 第一个用于英尺,第二个用于英寸 我想要它,如果值超过12,它将自动重置为0,高度应该增加1.

我该怎么做?

这是我到目前为止所做的:

height_seekbar.setProgress(68);
        height_feet_textview.setText(String.valueOf(5));
        height_inches_textview.setText(String.valueOf(8));
        height_seekbar
                .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {


                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                        // height_seekbar_textview.setText(seekBar.getProgress()
                        // + " ");
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                        // TODO Auto-generated method stub

                        //progress += 60;
                        int feet = progress / 12;
                        int inches = (progress % 12);

                        height_feet_textview.setText(String.valueOf(feet));
                        height_inches_textview.setText(String.valueOf(inches));

                        Log.i("BodyProfileHeightWeightActivity",
                                "Progress of seekbar" + progress);
                        // if (progress >= 12) {
                        //
                        // height_seekbar_textview.setText(0);
                        //
                        // }
                        // String heightSelected = String.valueOf(progress);
                        // height_seekbar_textview.setText(heightSelected);

                    }
                });

2 个答案:

答案 0 :(得分:1)

应用以下逻辑:

@Override
public void onProgressChanged(SeekBar seekBar,
    int progress, boolean fromUser) {
    // TODO Auto-generated method stub

    progress += 60;

    int feet = progress / 12;
    int inches = progress % 12;

    txt_feet.setText(String.valueOf(feet));
    txt_inches.setText(String.valueOf(inches));
}

答案 1 :(得分:0)

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(progress>=12){
                seekBar.setProgress(0);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });