如何获取按钮的当前文本颜色?

时间:2012-07-16 09:59:02

标签: java android

如果该按钮的当前文字颜色为红色,我想检查按钮点击事件吗?

到目前为止我已经这样做了:

ColorStateList mList = gridcell.getTextColors();

            int col=mList.getDefaultColor();


            switch(col)
            {
            case Color.RED:
                Toast.makeText(getApplicationContext(), "RED",
                        Toast.LENGTH_SHORT).show();
            break;

            }

但是当我点击文本颜色为红色的按钮时,它会干任何东西,defaut颜色为白色,并且在所有te按钮中变为白色。我现在能做什么?

3 个答案:

答案 0 :(得分:2)

试试这个......

ColorStateList mList = mButton.getTextColors();
int color = mList.getDefaultColor();

switch(color)
{
case Color.RED:
Toast.makeText(getApplicationContext(), "RED", Toast.LENGTH_SHORT).show();

break;

case Color.BLUE:
Toast.makeText(getApplicationContext(), "BLUE", Toast.LENGTH_SHORT).show();
break;

}

答案 1 :(得分:0)

请检查此

 ColorStateList mList = mButton.getTextColors();
 int col = mList.getDefaultColor();

 switch(col){
 case Color.RED:
 Toast.makeText(getApplicationContext(), "RED", Toast.LENGTH_SHORT).show();
 break;

 case Color.BLACK:
 Toast.makeText(getApplicationContext(), "BLACK", Toast.LENGTH_SHORT).show();
 break;

}

答案 2 :(得分:0)

试试这个:

        Button button=(Button) findViewById(R.id.b);
        button.setTextColor(Color.RED);
        button.setOnClickListener(this);

按钮的点击事件:

public void onClick(View v) {
        if(button.getCurrentTextColor()==Color.RED){
            Toast.makeText(getBaseContext(), "Red", Toast.LENGTH_SHORT).show();
        }
    }