ProgressBar值不会更改

时间:2017-01-17 17:11:43

标签: java android android-audiomanager android-seekbar

我想在内部类之外使用onProgressChanged方法的progress变量。我尝试使用外部类中定义的方法,并从内部类调用以获取该值。但每次我得到0.
没有更新的进度变量值。它基本上是一个获取SeekBar更新值的Android应用程序 看看代码以便更好地理解。目前我只获得上一个/最后一个进展值。我改变它时没有立即获得更新值。

Button gene;
ImageView image;
SeekBar ring_bar;
String collect;
private AudioManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prof);

    gene = (Button) findViewById(R.id.gene);
    image = (ImageView) findViewById(R.id.image);
    ring_bar = (SeekBar) findViewById(R.id.ring_bar);
    am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    int maxV = am.getStreamMaxVolume(AudioManager.STREAM_RING);
    int curV = am.getStreamVolume(AudioManager.STREAM_RING);
    ring_bar.setMax(maxV);
    ring_bar.setProgress(curV);
    ring_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
            am.setStreamVolume(AudioManager.STREAM_RING, progress, 0);
        }
    });

    int ring = ring_bar.getProgress();

    String ringgg = Integer.toString(ring);

    collect = ringgg;

    gene.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try{
                BitMatrix bitMatrix = multiFormatWriter.encode(collect, BarcodeFormat.QR_CODE,200,200);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                image.setImageBitmap(bitmap);
            }
            catch (WriterException e){
                e.printStackTrace();
            }

        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

您的问题是,您将onCreate中的进度保存到变量中,但之后您永远不会更新该值。

@Override
protected void onCreate(Bundle savedInstanceState) {
//....

int ring = ring_bar.getProgress();//Progress at the time of onCreate

gene.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        try{
           //collect is still the same value since onCreate 
            BitMatrix bitMatrix = multiFormatWriter.encode(collect, BarcodeFormat.QR_CODE,200,200);
            //....
        }
        catch (WriterException e){
            e.printStackTrace();
        }

    }
});

要为当前值创建QR,您必须在onClickListener中调用getProgress()。这将返回进度条的当前值。

gene.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        try{
            int ringtoneVolumeValue = ring_bar.getProgress();
            String ringtoneVolume = Integer.toString(ringtoneVolumeValue);
            BitMatrix bitMatrix = multiFormatWriter.encode(ringtoneVolume, BarcodeFormat.QR_CODE,200,200);
            //....
        }
        catch (WriterException e){
            e.printStackTrace();
        }

    }
});