关于计时器的Android代码

时间:2015-04-08 09:00:43

标签: android

我将代码更改为以下,我的目的是设置一个按钮 当我点击计时器运行时,如何更改下面的代码?谢谢 下面的新代码

我将代码更改为以下,我的目的是设置一个按钮 当我点击计时器运行时,如何更改下面的代码?谢谢 下面的新代码

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    Handler aHandler;
    TextView aTextView;
    Button  aButton;
    EditText aEditText;
    Handler aHandler01;
    TextView aTextView01;
    Button  aButton01;
    EditText aEditText01;
    Boolean checker=false;
    int count = 11;
    int count01 = 11;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        aEditText = (EditText)findViewById(R.id.editText01);
        aTextView = (TextView)findViewById(R.id.TextView01);
        aEditText01 = (EditText)findViewById(R.id.editText02);
        aTextView01 = (TextView)findViewById(R.id.TextView02);
        aButton=(Button)findViewById(R.id.button01);
        aButton01=(Button)findViewById(R.id.button02);
  //第一個計時器

        aButton.setOnClickListener(new Button.OnClickListener(){



            @Override
            public void onClick(View v){ if(!aEditText.getText().toString().equalsIgnoreCase(""))
            {count=Integer.parseInt(aEditText.getText().toString());}
                aHandler = new Handler();

                if(checker==false){

                    aHandler.post(runnable);
                    checker=true;
                    }
            }
        });
//第二個計時器
        aButton01.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!aEditText01.getText().toString().equalsIgnoreCase("")) {
                    count01 = Integer.parseInt(aEditText01.getText().toString());
                }
                aHandler01 = new Handler();
                aHandler01.post(runnable01);
            }
        });




    }

    //第一個計時器
    final Runnable runnable = new Runnable() {
        public void run() { // TODO Auto-generated method stub

            if (count> 0) {
                aTextView.setText(Integer.toString(count-1));
                count--;
                aHandler.postDelayed(runnable, 1000);
            }else{
                aTextView.setText("boom");
            }
        }
    };
    //第二個計時器
    final Runnable runnable01 = new Runnable() {
        public void run() { // TODO Auto-generated method stub

            if (count01> 0) {
                aTextView01.setText(Integer.toString(count01-1));
                count01--;
                aHandler01.postDelayed(runnable01, 1000);
            }else{
                aTextView01.setText("boom");
            }
        }
    };



        @Override
    protected void onPause() { // TODO Auto-generated method stub
        super.onPause();
        if (aHandler != null) {
            aHandler.removeCallbacks(runnable);
        }




    }
}

2 个答案:

答案 0 :(得分:1)

试试这个

 private Handler handler = new Handler();
    Boolean checker=false;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        aEditText = (EditText)findViewById(R.id.editText01);
        aTextView = (TextView)findViewById(R.id.TextView01);
        aButton=(Button)findViewById(R.id.button01);
        aButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){ if(!aEditText.getText().toString().equalsIgnoreCase(""))
            {count=Integer.parseInt(aEditText.getText().toString());}



 if(!checker){
             checker=true;
            handler.post(runnable);//to start timer


            }


            }
        });



    }



    final Runnable runnable = new Runnable() {
            public void run() {
    // TODO Auto-generated method stub


  if(checker)
    {
    checker=false;
     return;
    }
                if (count> 0) {
                    aTextView.setText(""+Integer.toString(count-1));
                    count--;
                    aHandler.postDelayed(runnable, 1000);
                }else{
                    aTextView.setText("boom");
                }
            }
        };

答案 1 :(得分:0)

当你在editText为空时从editText中将值赋值给OnCreate中的'count'变量时,它返回null字符串,然后你解析int值,返回NumberFormatException

尝试按以下方式进行操作

if (!aEditText.getText().toString().equalsIgnoreCase(""))
    {
        count = Integer.parseInt(aEditText.getText().toString());
    }

或者您可以在任何按钮单击时使用editText为'count'赋值,或者使用TextChangedListener(TextWatcher)在运行时监视并从edittext获取文本。