setOnchronometerTickListener():为计时器绑定时间监听器,当计时器改变时触发该监听器
计时20s,当时间到了则停止计时。
MainActivity.java:
import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener;; public class MainActivity extends Activity { private Chronometer ch; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ch = (Chronometer) findViewById(R.id.test); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //设置开始时间 ch.setBase(SystemClock.elapsedRealtime()); //开始计时 ch.start(); //按钮不可用 button.setEnabled(false); } }); ch.setOnChronometerTickListener(new OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { // TODO Auto-generated method stub //如果计时超过20s if (SystemClock.elapsedRealtime() - ch.getBase() > 20*1000){ //停止计时 ch.stop(); //按钮可用 button.setEnabled(true); } } }); } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_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.chronometerdemo2.MainActivity" > <Chronometer android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动" /> </LinearLayout>
用户自定义计时时间:
MainActivity.java:
import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; public class MainActivity extends Activity implements OnClickListener { private Chronometer ch; private Button start, stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ch = (Chronometer) findViewById(R.id.test); start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.id.stop); start.setOnClickListener(this); stop.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.start: ch.setBase(SystemClock.elapsedRealtime()); ch.start(); start.setEnabled(false); break; case R.id.stop: ch.stop(); start.setEnabled(true); break; default: break; } } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_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.chronometerdemo.MainActivity" > <Chronometer android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout> </LinearLayout>
下面是我自己制作的一个计时器,但是有个问题,望看到的大神能给解答一下。。
MainActivity.java:
import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button start, stop, reset; private Chronometer ch; private TextView show; private String tip = ""; private int num = 0; private Boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ch = (Chronometer) findViewById(R.id.ch); start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.id.stop); reset = (Button) findViewById(R.id.reset); start.setOnClickListener(this); stop.setOnClickListener(this); reset.setOnClickListener(this); show = (TextView) findViewById(R.id.show); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.start: ch.setBase(SystemClock.elapsedRealtime()); ch.start(); start.setEnabled(false); flag = true; break; case R.id.stop: if (flag == true) { num++; long time = SystemClock.elapsedRealtime() - ch.getBase(); tip += num + "计次 " + time / 1000 + "." + time % 1000 + "s \n"; show.setText(tip); } break; case R.id.reset: ch.stop(); flag = false; show.setText(""); num = 0; tip = ""; start.setEnabled(true); break; default: break; } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_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.chronometerdemo3.MainActivity" > <Chronometer android:id="@+id/ch" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="计时时长" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计次" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" /> </LinearLayout> </LinearLayout>
代码就是上面这些,问题呢就是我想将布局文件中的那个TextView放到内部嵌套的那个LinearLayout的下面,按照我的理解应该是没有问题,编译器也没有报错,但是运行在模拟器上的时候,却是闪退。。。不知道问题出在哪了。。。