在Handler的帮助下从单独的线程设置TextView的值

时间:2019-01-05 10:08:23

标签: java android multithreading

UPD

我有课程代码:

   const _7z = require('7zip-min');
    _7z.unpack('zip-7z-file-path', 'extract-directory', err => {
      if (err) {
        return cb(err);
      }
      cb(null, 'extract-directory');
    });

主要目的是将新值每秒package asus.example.com.player; import android.annotation.SuppressLint; import android.os.Handler; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener { private Button prev; private Button next; private Button play; private Snackbar snackbar; private SeekBar seekBar; private TextView curTime; private Handler handler = new Handler(); private final int SONG_LENGTH = 120; View.OnClickListener snackbarOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { snackbar.dismiss(); } }; private void onPrevClick(final View view){ Thread thread = new Thread(new Runnable() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { snackbar = Snackbar.make(view, "Prev", Snackbar.LENGTH_INDEFINITE).setAction("Close", snackbarOnClickListener); snackbar.show(); } }); } }); thread.run(); } private void onPlayClick(final View view){ Thread thread = new Thread(new Runnable() { @Override public void run() { int secs = seekBar.getProgress()*SONG_LENGTH/100; while (secs<=SONG_LENGTH) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } seekBar.setProgress(secs/SONG_LENGTH*100); secs++; } } }); thread.run(); } private void onNextClick(final View view){ Thread thread = new Thread(new Runnable() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { snackbar = Snackbar.make(view, "Next", Snackbar.LENGTH_INDEFINITE).setAction("Close", snackbarOnClickListener); snackbar.show(); } }); } }); thread.run(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); prev = findViewById(R.id.prev); play = findViewById(R.id.play); next = findViewById(R.id.next); seekBar = findViewById(R.id.seekBar); curTime = findViewById(R.id.curTime); prev.setOnClickListener(this); play.setOnClickListener(this); next.setOnClickListener(this); seekBar.setOnSeekBarChangeListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.prev: onPrevClick(v); break; case R.id.play: onPlayClick(v); break; case R.id.next: onNextClick(v); break; } } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { int seconds = (SONG_LENGTH*seekBar.getProgress()/100)%60; int minutes = (SONG_LENGTH*seekBar.getProgress()/100)/60; @SuppressLint("DefaultLocale") String result = String.format("%d:%02d", minutes, seconds); curTime.setText(result); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar){ } } 放入TextView。由于无法从secs<=SONG_LENGTH进行操作,因此我使用thread。当我遇到Handler这样的问题时,它可以帮助我,但是此时程序停止了。它没有关闭,但是我无法单击任何按钮SnackBar。有什么问题,如何解决?并且,如果这对您来说并不困难-请解释一下为什么它可以与SeekBar一起使用,但不能与SnackBar一起使用?

0 个答案:

没有答案