从seekbar java更新int值

6vl6ewon  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(312)

我有一个seekbar和一个在seekbar外定义的int。int的值是seekbar进程。一切正常,只是我希望int的值随着seekbar进程的变化而变化。现在,它只在程序启动时从seekbar获取值。

int tableNumber = seekBar.getProgress();
    SeekBar seekBar = findViewById(R.id.seekBar);

    seekBar.setMin(1);
    seekBar.setMax(20);
    seekBar.setProgress(10);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            Log.i("Number", Integer.toString(i));

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
jjjwad0x

jjjwad0x1#

声明 tableNumber 外部方法并在中设置其值 onProgressChanged ```
int tableNumber = 0; // declare above method

public void someInitiatingMethodMaybeOnCreate(){
SeekBar seekBar = findViewById(R.id.seekBar);
tableNumber = seekBar.getProgress(); // initial set

seekBar.setMin(1);
seekBar.setMax(20);
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        Log.i("Number", Integer.toString(i));
        tableNumber = i; // set current progress
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});

}

wyyhbhjk

wyyhbhjk2#

你必须更新 tableNumber 每次seekbar进程更改时都会发生变量。

public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        Log.i("Number", Integer.toString(i));
        tableNumber = i; // add this line 
    }

相关问题