标签:
智者创造机会,强者把握机会,弱者等待机会。
本讲内容:定时器
<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" tools:context="com.example.text1.MainActivity$PlaceholderFragment" > <Chronometer android:id="@+id/chronometer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:format="%s" android:gravity="center" android:textColor="#542039" android:textSize="25sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center" android:text="设置时间:" android:textSize="25sp"/> <EditText android:id="@+id/settime" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:inputType="number"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <Button android:id="@+id/start" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="开始记时"/> <Button android:id="@+id/stop" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止记时" /> <Button android:id="@+id/reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="重置"/> </LinearLayout> </LinearLayout>
public class MainActivity extends Activity implements OnClickListener{ private Button star; private Button stop; private Button reset; private EditText settime; private Chronometer chronometer; private int startTime=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); // 设置开始讲时时间 chronometer.setBase(SystemClock.elapsedRealtime()); // 开始记时 chronometer.start(); chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { // 如果开始计时到现在超过了startime秒 if(SystemClock.elapsedRealtime()-chronometer.getBase()>10*1000){ chronometer.stop(); //给用户提示 showDialog(); } } }); } private void findViews() { star=(Button) findViewById(R.id.start); stop=(Button) findViewById(R.id.stop); reset=(Button) findViewById(R.id.reset); settime=(EditText) findViewById(R.id.settime); chronometer=(Chronometer) findViewById(R.id.chronometer); star.setOnClickListener(this); stop.setOnClickListener(this); reset.setOnClickListener(this); } private void showDialog() { AlertDialog.Builder builder=new Builder(this); builder.setIcon(R.drawable.ic_launcher).setTitle("警告").setMessage("时间到"); builder.setPositiveButton("确定", null).setNegativeButton("取消", null).show(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: String ss=settime.getText().toString(); if(!(ss.equals("")&&ss!=null)){ startTime=Integer.parseInt(ss); } // 设置开始讲时时间 chronometer.setBase(SystemClock.elapsedRealtime()); // 开始记时 chronometer.start(); break; case R.id.stop: chronometer.stop(); break; case R.id.reset: chronometer.setBase(SystemClock.elapsedRealtime()); break; } } }
本讲就到这里,Take your time and enjoy it
标签:
原文地址:http://blog.csdn.net/liguojin1230/article/details/43205003