标签:android
感谢极客学院的视频
布局文件:
<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+""自动转化
本文出自 “Chronosphere” 博客,转载请与作者联系!
标签:android
原文地址:http://chronosphere.blog.51cto.com/9598252/1616600