我正在努力使按钮将textView的颜色更改为WHITE,但是在第二次按下时将其更改为BLUE,然后按第三次按下RED等等。当我尝试它时,显然它只选择最后一个(RED)并且不会再次改变。任何人有任何想法我怎么能这样做?真的卡住了。这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
dc1 = (TextView)findViewById(R.id.digitalClock1);
bt1 = (Button)findViewById(R.id.button1);
Handler handler = new RandomMoveHandler((TextView) findViewById(R.id.digitalClock1));
handler.sendEmptyMessage(0);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dc1.setTextColor(Color.WHITE);
dc1.setTextColor(Color.BLUE);
dc1.setTextColor(Color.RED);
}
});
}
答案 0 :(得分:1)
private int count = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
dc1 = (TextView)findViewById(R.id.digitalClock1);
bt1 = (Button)findViewById(R.id.button1);
Handler handler = new RandomMoveHandler((TextView) findViewById(R.id.digitalClock1));
handler.sendEmptyMessage(0);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(count)
{
case 0: dc1.setTextColor(Color.WHITE); count++; break;
case 1: dc1.setTextColor(Color.BLUE); count++; break;
case 2: dc1.setTextColor(Color.RED); count = 0; break;
}
}
});
答案 1 :(得分:0)
int[] colors = {Color.WHITE, Color.BLUE, Color.RED };
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer tag = (v.getTag() == null) ? 0 : (Integer)v.getTag();
int color = colors[tag];
dc1.setTextColor(color);
v.setTag((tag+1)%colors.length);
}
});
我现在不能尝试。检查拼写错误
答案 2 :(得分:0)
试试这个:
int [] colors = new int[]{Color.WHITE, Color.BLUE, Color.RED};
int index = 0;
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dc1.setTextColor(colors[index]);
index++;
if(index == colors.length)
index = 0;
}
});
答案 3 :(得分:0)
好吧,看起来像一个初学者的问题。
你现在正在做的是当你点击按钮时,调用onClick()
方法,然后将颜色设置为白色,然后是蓝色,然后是眨眼间的红色。
你想做的是更像其他答案
答案 4 :(得分:0)
你需要计算点击次数,试试这个:
private int flag = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main2);
dc1 = (TextView) findViewById(R.id.digitalClock1);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (flag == 0)
dc1.setTextColor(Color.WHITE);
else if (flag == 1)
dc1.setTextColor(Color.BLUE);
else if (flag == 2)
dc1.setTextColor(Color.RED);
if (flag++ >= 2)
flag = 0;
}
});