-1

I'm having a problem to change the button color after a certain amount of time. I know how to change using handle after a fixed time, but I need to change the color after a specific time that is chosen by the user.

public class MainActivity extends Activity {

EditText tempo;
Button bt;
int estado = 1;


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

    tempo = (EditText) findViewById(R.id.tempo);
    long delay = Long.parseLong(tempo.getText().toString());


     bt = (Button) findViewById(R.id.btvibrar);

     bt.setOnClickListener(new View.OnClickListener() {


        public void onClick(View arg0) {


        if(!tempo.getText().toString().equals("")){

            if(estado==1){

                  Vibrar();
                  estado*=-1;

                 bt.setText("Parar !");
                 bt.setBackgroundColor(Color.RED);

                    //Handler handler = new Handler();
                    //handler.postDelayed(new Runnable() {

                        //@Override
                        //public void run() {
                            //estado*=-1;
                            //bt.setText("Vibrar !");
                             //bt.setBackgroundColor(Color.GREEN);
                        //}
                //  },  );
                            } else {
                                Parar();
                                estado*=-1;
                                bt.setText("Vibrar !");
                                 bt.setBackgroundColor(Color.GREEN);

                                    }
                                        } else  {
                                            AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this);
                                            dialogo.setTitle("Erro !");
                                            dialogo.setMessage("Escolha um tempo.");
                                            dialogo.setNeutralButton("OK", null);
                                            dialogo.show();

                                        }
        }



        private void Vibrar(){    // É necessario lançar excessao no ANDROIDMANIFEST.XML
            Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            long treal = Long.parseLong(tempo.getText().toString());
            long milliseconds = treal*1000;  
            rr.vibrate(milliseconds);
        }

        private void Parar(){
            Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            rr.cancel();
        }

    });
    }

}

4

1 回答 1

0

此代码将按钮的颜色切换为红色:

bt.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        new AsyncTask<Long, Void, Void>() {
            @Override
            protected Void doInBackground(Long... delay) {
                if (delay.length > 0) {
                    try {
                        Thread.sleep(delay[0]);

                    } catch (InterruptedException e) {
                    }
                }

                return null;
            }

            @Override
            protected void onProgressUpdate(Void... none) {
            }

            @Override
            protected void onPostExecute(Void none) {
                if (ContactManager.this != null && !ContactManager.this.isFinishing()) {
                    bt.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
                    bt.invalidate();
                }
            }
        }.execute(new Long(delay));
    }
});

它使用delay延迟变量。希望这可以帮助 ...

干杯!

于 2013-04-19T20:44:59.867 回答