1
我想從SeekBar
中寫入值來登錄新線程。我想按下按鈕並開始寫入日誌。並更改seekBar值並將其寫入日誌中。Android在新線程中每秒寫入一個日誌
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, View.OnTouchListener {
private SeekBar seekBar;
private Button button;
private TextView textView;
private EditText editText;
private Thread thread;
private int value = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
seekBar.setOnSeekBarChangeListener(this);
button.setOnTouchListener(this);
print();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
print();
Toast.makeText(MainActivity.this, "ACTION_DOWN", Toast.LENGTH_SHORT).show();
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
thread.stop();
Toast.makeText(MainActivity.this, "ACTION_UP", Toast.LENGTH_SHORT).show();
return true; // if you want to handle the touch event
}
return false;
}
public void print() {
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d("MyLog", String.valueOf(value));
}
};
thread = new Thread(runnable);
thread.start();
}
}
但我的日誌寫入一次。我怎樣才能每秒鐘寫日誌?