感謝極客學院的視頻
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.timer.MainActivity" > <EditText android:id="@+id/inputtime" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/settime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="set" /> <TextView android:id="@+id/showtime" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" > <Button android:id="@+id/starttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" /> <Button android:id="@+id/endtime" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="end" /> </LinearLayout> </LinearLayout>
下面是Java文件:
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener{
EditText inputtime;
Button settime,starttime,endtime;
TextView showtime;
private int i = 0 ;
Timer timer = null;
TimerTask task = null;
private void init(){
inputtime = (EditText)findViewById(R.id.inputtime);
settime = (Button)findViewById(R.id.settime);
starttime = (Button)findViewById(R.id.starttime);
endtime = (Button)findViewById(R.id.endtime);
showtime = (TextView)findViewById(R.id.showtime);
settime.setOnClickListener(this);
starttime.setOnClickListener(this);
endtime.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.settime:
showtime.setText(inputtime.getText().toString());
i = Integer.parseInt(inputtime.getText().toString());
break;
case R.id.starttime:
startTime();
break;
case R.id.endtime:
endTime();
break;
}
}
private Handler mHandler = new Handler(){
public void handleMessage(Message msg){
showtime.setText(msg.arg1+"");
startTime();
}
};
public void startTime(){
timer = new Timer();
task = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
};
timer.schedule(task,1000);//第二個參數表示以1000毫秒計時
}
public void endTime(){
timer.cancel();
}
}設置監聽可以implements OnClickListener接口然后重寫onClick(View v)方法,然后給組件widget.setOnClickListener(this);然后在onClick()方法里抓取v.getId()
TextView.setText(xxxx)里xxxx必須是字符串類型否則崩潰,可以用xxx.toString()也可以xxx+""自動轉化
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。