如何重置搜索栏

时间:2017-06-13 22:52:30

标签: java android

我有一个包含10首歌曲的节目 当我点击其中一首歌时,搜索栏开始移动 但问题是,当我点击另一首歌时,搜索栏没有返回并再次开始移动它只停在第一首歌停止的地方

那么当我点击另一首歌时如何重置搜索栏

process (clk) begin 
    if falling_edge(clk) then
        destination <= source;

我尝试在每个onClick上执行此方法

    public class on extends AppCompatActivity {
MediaPlayer x;
int tx1;
mythread my = new mythread();
private SeekBar seekbar;
class mythread extends Thread {
    public void run() {
        while (x != null) {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            seekbar.post(new Runnable() {
                @Override
                public void run() {
                    seekbar.setProgress(x.getCurrentPosition());
                }
            });
        }
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_on);
    Button a = (Button) findViewById(R.id.button1);
    Button b = (Button) findViewById(R.id.button2);
    Button c = (Button) findViewById(R.id.button3);
    Button d = (Button) findViewById(R.id.button4);
    Button e = (Button) findViewById(R.id.button5);
    Button f = (Button) findViewById(R.id.button6);
    Button g = (Button) findViewById(R.id.button7);
    Button h = (Button) findViewById(R.id.button8);
    Button i = (Button) findViewById(R.id.button9);
    Button j = (Button) findViewById(R.id.button10);
    seekbar = (SeekBar) findViewById(R.id.seekBar1);
    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            tx1 = progress;
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            x.seekTo(tx1);
        }
    });
    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onone);
            seekbar.setMax(x.getDuration());
            x.start();
            my.start();
        }

    });
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.ontwo);
            seekbar.setMax(x.getDuration());
            x.start();
            my.start();
        }
    });

    c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onthree);
            seekbar.setMax(x.getDuration());
            x.start();
            my.start();
        }
    });
    d.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onfour);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
    e.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onfive);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
    f.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onsix);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
    g.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onseven);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
    h.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.oneight);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
    i.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onnine);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
    j.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopPlaying();
            x = MediaPlayer.create(on.this, R.raw.onten);
            seekbar.setMax(x.getDuration());
            my.start();
            x.start();
        }
    });
}
@Override
public void onDestroy() {
    super.onDestroy();
    if (x != null) {
        x.stop();
    }
}

private void stopPlaying() {
    if (x != null) {
        x.stop();
        x.release();
        x = null;
    }
}}

1 个答案:

答案 0 :(得分:0)

通常会使用

seekbar.setProgress(0);

将进度条重置为初始位置。

我发现您已将代码发布为答案,而不是编辑问题。

您的代码的一个大问题是您不止一次致电Thread.start(),这是非法的。

此外,我不会在单独的线程中更新ProgressBar,而是可以在UI线程上执行此操作。

可以在此问题MediaPlayer, ProgressBar

中阅读替代方法