0

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

    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

3 回答 3

0

Sorry for being late, here is a full example:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/etTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="28dp"
    android:text="Start" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_marginLeft="53dp"
    android:layout_toRightOf="@+id/button1"
    android:ems="10"
    android:inputType="number|none"
    android:digits="1234567890" >

    <requestFocus />
</EditText>

MainActivity.java

package com.example.testevent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText etTime;
Button bStart;
myHandler mh;
int time;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bStart = (Button) this.findViewById(R.id.button1);
    etTime = (EditText) this.findViewById(R.id.editText1);

    bStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            mh = new myHandler(bStart);
            try {
                time = Integer.parseInt(etTime.getText().toString());
            } catch (Exception e) {
                time = 1000;
            }
            tester a = new tester(mh, time);
            a.start();

        }
    });
}

}

class myHandler extends Handler {
Button bStart;

public myHandler(Button bStart) {
    this.bStart = bStart;
}

public void DisplayResult(String Result) {
    // error management,creates an error message
    Message msg = obtainMessage(1);

    // sends the message to our handler
    sendMessage(msg);
}

public void handleMessage(Message msg) {
    // switch (msg.what)
    bStart.setBackgroundColor(Color.RED);
}
}

class tester extends Thread {
myHandler mh;
int time;

public tester(myHandler mh, int time) {
    this.mh = mh;
    this.time = time;
}

@Override
public void run() {
    try {
        Thread.sleep(time);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // error management,creates an error message
    Message msg = mh.obtainMessage(1);

    // sends the message to our handler
    mh.sendMessage(msg);
}
}
于 2013-04-22T09:53:27.333 回答
0

例如,在按钮侦听器中尝试此操作:

        try {
            Thread.sleep(_time_);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Button but = (Button)theMainActiv.findViewById(R.id.bSend);
        but.setBackgroundColor(Color.BLUE);

_time_你可以从Edittext例如

于 2013-04-19T17:07:54.307 回答
0

注意你输入了多少睡眠时间——超过 5 秒,系统会给你一个弹出窗口,告诉用户你的应用程序挂起并提供关闭它。

于 2013-04-19T17:51:10.680 回答