package com.example.ex00_android;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private TextView mTv_CurrTime;
private TextView mTv_TotalTime;
private SeekBar mSeekBar;
private ImageButton mButton;
//總時間
private int totaltime;
//當前時間
private int currtime;
private boolean isplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//確定總時間,並轉換成“00:00”格式
totaltime = 235;
String str_totaltime = timeFormat(totaltime);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
mTv_CurrTime = (TextView) findViewById(R.id.tv_curr);
mTv_TotalTime = (TextView) findViewById(R.id.tv_total);
mTv_TotalTime.setText(str_totaltime);
mSeekBar = (SeekBar) findViewById(R.id.seekBar1);
//設置seekbar進度總共分爲幾等分
mSeekBar.setMax(totaltime);
mButton = (ImageButton) findViewById(R.id.imageButton1);
mButton.setOnClickListener(this);
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
currtime = progress;
String string = timeFormat(currtime);
mTv_CurrTime.setText(string);
}
});
}
//轉換時間格式的方法
private String timeFormat(int time)
{
int second = time % 60;
int minute = time / 60;
StringBuffer buffer = new StringBuffer();
if(minute < 10)
{
buffer.append(0);
}
buffer.append(minute).append(":");
if(second < 10)
{
buffer.append(0);
}
buffer.append(second);
return buffer.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
btnOclick1();
break;
case R.id.button2:
btnOclick2();
break;
case R.id.imageButton1:
play();
break;
default:
break;
}
}
private Runnable action;
private void play() {
isplay = !isplay;
if(isplay)
{
mButton.setImageResource(R.drawable.ic_pause);
new Thread()
{
public void run()
{
action = new Runnable() {
@Override
public void run() {
if(!isplay)
{
return;
}
currtime ++;
String str_currtime = timeFormat(currtime);
mTv_CurrTime.setText(str_currtime);
mSeekBar.setProgress(currtime);
if(currtime >= totaltime)
{
isplay = false;
mButton.setImageResource(R.drawable.ic_play);
currtime = 0;
mSeekBar.setProgress(currtime);
mTv_CurrTime.setText("00:00");
}else
{
mTv_CurrTime.postDelayed(action, 1000);
}
}
};
mTv_CurrTime.post(action );
};
}.start();
}
else
{
mButton.setImageResource(R.drawable.ic_play);
}
}
private void btnOclick2() {
Log.e("btnOclick2", "btnOclick2");
}
private void btnOclick1() {
Log.e("btnOclick1", "btnOclick1");
}
}