Android 开发第一弹:倒计时

版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/48633843

好吧……我承认很尴尬……

但毕竟作为开端,还是将这个贴出来,以后一定写出厉害的!

<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=".MainActivity">

    <EditText
        android:layout_width="197dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etInputNum" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取计时计数"
        android:id="@+id/btnGetNum"
        android:layout_gravity="center_horizontal" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="109dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始计时"
            android:id="@+id/btnStartCount"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="56dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="结束计时"
            android:id="@+id/btnStopCount"
            android:layout_alignTop="@+id/btnStartCount"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="59dp" />
    </RelativeLayout>

    <TextView
        android:id="@+id/tvOutputNum"
        android:layout_width="match_parent"
        android:layout_height="208dp" />

</LinearLayout>
package myapplication.nomasp.com.timer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity implements View.OnClickListener {

    private EditText etInputNum,etOutputNum;
    private Button btnGetNum,btnStartCount,btnStopCount;
    private TextView tvOutputNum;
    private  int i=0;
    private Timer timer=null;
    private TimerTask task=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        etInputNum=(EditText)findViewById(R.id.etInputNum);
        btnGetNum=(Button)findViewById(R.id.btnGetNum);
        btnStartCount=(Button)findViewById(R.id.btnStartCount);
        btnStopCount=(Button)findViewById(R.id.btnStopCount);
        tvOutputNum=(TextView)findViewById(R.id.tvOutputNum);
        btnGetNum.setOnClickListener(this);
        btnStartCount.setOnClickListener(this);
        btnStopCount.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btnGetNum:
                tvOutputNum.setText(etInputNum.getText().toString());
                i=Integer.parseInt(etInputNum.getText().toString());
                break;
            case R.id.btnStartCount:
                startTime();
                break;
            case R.id.btnStopCount:
                stopTime();
                break;
            default:
                break;
        }
    }

    private Handler mHandler=new Handler(){
        public void handleMessage(Message msg){
            tvOutputNum.setText(msg.arg1+"");
            startTime();
        };
    };

    public void startTime(){
        timer=new Timer();
        task =new TimerTask() {
            @Override
            public void run() {
                i--;
                Message message=mHandler.obtainMessage();
                message.arg1=i;
                mHandler.sendMessage(message);
            }
        };
        timer.schedule(task,1000);
    }

    public void stopTime(){
        timer.cancel();
    }
}

加油!

没有更多推荐了,返回首页