我想动态更改按钮颜色。
在编写,测试和研究之后,我发现显示线程应该与代码不同,所以我使用了处理程序。
我试图将按钮的颜色从默认更改为黑色一段时间,然后将其恢复为默认颜色, 但它没有发生,它只显示一个默认按钮。
以下是我的代码
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.Random;
import android.os.Handler ;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button b1;
Random r = new Random();
int random_selection;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(this);
main();
}
public void main() {
// Thread t1 = new Thread(new MainActivity());
// this wi run() function
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
int count = 0;
try {
System.out.println("inside run");
for (i=0;i<4 ;i++ )
// android.widget.Toast.makeText(getBaseContext(), count,android.widget.Toast.LENGTH_SHORT).show();
Handler handler2 = new Handler();
int x = randomgen();
// android.widget.Toast.makeText(getBaseContext(), x,android.widget.Toast.LENGTH_SHORT).show();
if (x == 1) {
b1.setBackgroundColor(Color.BLACK);
handler2.postDelayed(new Runnable() {
@Override
public void run() {
b1.setBackgroundResource(android.R.drawable.btn_default);
}
}, 10000);
} else if (x == 2) {
b2.setBackgroundColor(Color.BLACK);
handler2.postDelayed(new Runnable() {
@Override
public void run() {
b2.setBackgroundResource(android.R.drawable.btn_default);
}
}, 2000);
} else if (x == 3) {
b3.setBackgroundColor(Color.BLACK);
handler2.postDelayed(new Runnable() {
@Override
public void run() {
b3.setBackgroundResource(android.R.drawable.btn_default);
}
}, 2000);
} else if (x == 4) {
b4.setBackgroundColor(Color.BLACK);
handler2.postDelayed(new Runnable() {
@Override
public void run() {
b4.setBackgroundResource(android.R.drawable.btn_default);
}
}, 2000);
}
}
}catch (Exception e) {
System.out.println("inside catch");
e.printStackTrace();
}
}
}, 1000 );
}
public int randomgen() {
random_selection = r.nextInt(4) + 1;
return random_selection;
}
答案 0 :(得分:0)
原因是你的b1.setBackgroundResource(android.R.drawable.btn_default);
在延迟处理程序之外,
将您的功能main
更改为:
public void main() {
int count = 0;
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
Handler handler2 = new Handler();
@Override
public void run() {
try {
System.out.println("inside run");
// android.widget.Toast.makeText(getBaseContext(), x,android.widget.Toast.LENGTH_SHORT).show();
b1.setBackgroundColor(Color.BLACK);
handler2.postDelayed(new Runnable() {
@Override
public void run() {
b1.setBackgroundResource(android.R.drawable.btn_default);
}
}, 2000);
} catch (Exception e) {
System.out.println("inside catch");
e.printStackTrace();
}
}
}, 1000);
}
答案 1 :(得分:0)
改为使用线程:
b1.setBackgroundColor(Color.BLACK);
new Thread(new Runnable() {
@Override
public void run() {
try { System.sleep(2000) } catch(Exception e) {}
runOnUiThread(new Runnable() {
@Override
public void run() {
b1.setBackgroundResource(android.R.drawable.btn_default);
}
});
}
}).start();