我在一段时间后更改按钮颜色时遇到问题。我知道如何在固定时间后更改使用句柄,但我需要在用户选择的特定时间后更改颜色。
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();
}
});
}
}
答案 0 :(得分: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
获取它,例如
答案 1 :(得分:0)
答案 2 :(得分:0)
抱歉迟到了,这是一个完整的例子:
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);
}
}