【问题标题】:Change the button color after a variable time在可变时间后更改按钮颜色
【发布时间】:2013-04-19 16:06:42
【问题描述】:

我在一段时间后无法更改按钮颜色。我知道如何在固定时间后使用句柄进行更改,但我需要在用户选择的特定时间后更改颜色。

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();
        }

    });
    }

}

【问题讨论】:

  • 我现在发布了,但我认为我犯了一些错误,我的帖子被关闭了。这是一个振动手机的应用程序。
  • 你还不清楚是什么问题。
  • 对不起,我是第一次来,这个应用可以震动手机,用户选择时间。当时间结束时,我想让我的按钮再次变绿(自动),所以我需要等待一定的时间(用户选择的时间,再次将按钮变绿)

标签: android android-handler


【解决方案1】:

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

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 变量作为延迟。希望这会有所帮助...

干杯!

【讨论】:

  • 没用。我想我可能做错了什么。在我的代码中,我必须把你的放在哪里?
  • Mmmmhhh... 我在一个示例应用程序中对其进行了测试,它运行良好。您必须在onCreate() 中为您要更改的按钮设置监听器(这里它被命名为bt)。所以在上面的代码之前添加一个Button bt = (Button)findViewbyId(...。你有任何例外吗?如果是:是哪一个?
  • 我认为唯一的例外是当用户没有填写 EditText.. 并且可能当用户关闭应用程序时我想停止振动。我怎么能直接和你说话并解释?对不起,我刚开始做这件事,真的谢谢你。你能把你的电子邮件发给我吗?
  • 如果您感到舒适,请给我发送电子邮件:Rcunhagoncalves@gmail.com
  • 我刚看到,它在跨度上。
猜你喜欢
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2015-11-23
相关资源
最近更新 更多