Android:在SeekBar中设置时间间隔

问题描述

| 在我的应用程序中,对话框中显示了“ 0”。 现在我想设置间隔并进入
Seekbar
。间隔应为0-250,每个步骤应为5分钟。 到目前为止,这是我的代码:
public class seekActivity extends Activity implements OnClickListener,OnSeekBarChangeListener {
    SeekBar seekbar;
    Button button;
    TextView textview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set up main content view
        setContentView(R.layout.main);
        //this button will show the dialog
        Button button1main = (Button) findViewById(R.id.Button01main);
        button1main.setOnClickListener(this);
    }
    public void onClick(View v) {
        //set up dialog
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.maindialog);
        dialog.setTitle(\"This is my custom dialog box\");
        dialog.setCancelable(true);
        button = (Button) dialog.findViewById(R.id.Button01);
        seekbar = (SeekBar) dialog.findViewById(R.id.seekBar1);
        seekbar.setOnSeekBarChangeListener(this);
        textview = (TextView) dialog.findViewById(R.id.textView1);
        dialog.show();
    }
    public class SeekBarPreference {
    }
    @Override
    public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
        // TODO Auto-generated method stub
        textview.setText(progress + \"\");
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    };
}
谁能给我一些提示/代码怎么做? 谢谢!     

解决方法

        而不是这样做,为什么不只将SeekBar的最大值设置为50(250/5),然后执行从SeekBar值到\“ real \”值的任何必要转换?     ,        您只需按如下所示更新onProgressChanged方法。
int stepSize = 5;
public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
    progress = ((int)Math.round(progress/stepSize))*stepSize;
    seekBar.setProgress(progress);
    textview.setText(progress + \"\");

}
    ,        如果您使用
Math.round((float)progress / stepSize) * stepSize
您的阈值将处于两个步骤的中间 如果您使用
progress / stepSize * stepSize
滑块将始终跳到左侧。这只会减少除法中的小数。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...