我正在考虑在android中制作一个计时器 这是主要代码。
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv1 = (TextView)findViewById(R.id.host_place);
final incrementTimer k = new incrementTimer();
tv1.setText("00:00");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
int i=0;
while(i!=2){
try{
String alteredTime = k.startTimer(tv1.getText().toString());
tv1.setText(alteredTime);
Thread.sleep(1000);}
catch(InterruptedException ie)
{
tv1.setText(ie+"");
}i++;}
}
});
}
和incrementTimer()代码是:
public class incrementTimer {
public String startTimer(String currTime) throws InterruptedException {
int zPos = Integer.parseInt(""+currTime.charAt(0));
int fPos = Integer.parseInt(""+currTime.charAt(1));
int sPos = Integer.parseInt(""+currTime.charAt(3));
int tPos = Integer.parseInt(""+currTime.charAt(4));
int hh = zPos*10 + fPos;
int mi = sPos*10 + tPos;
//----Incrementing minutes
if(mi==60){
hh+=1;
mi=0;
}
else
mi+=1;
if(hh==24) {
hh = 0;
}
if(mi<10 && hh<10){
return "0"+hh+":0"+mi+"";
}
if(hh<10){
return "0"+hh+":"+mi+"";
}
if(mi<10){
return ""+hh+":0"+mi+"";
}
return ""+hh+":"+mi+"";
}
}