Android / Java:创建颜色的字符串?

时间:2014-07-27 23:21:01

标签: java android string colors

我目前正在尝试根据STROOP EFFECT制作游戏。

在游戏中,String在Textview中显示。然后,用户必须选择字符串的颜色而不是单词。

我试图给用户两个选项供选择(2个按钮):

  1. TextView
  2. 中显示的字符串
  3. Textview
  4. 颜色的字符串表示形式

    目前我的应用程序适用于String,但对于颜色,它给出了颜色的int值。

    如何将此int值转换为用户的字符串?我已经看到了类似问题的答案here,但它不一样。

    当前输出的e.g,请注意下面我实际上想要的数字是“蓝色”而不是-16776961(单词的颜色):

    enter image description here

    我目前使用以下方式尝试执行此操作失败:

    btn2.setText("" + colorsOnScreen[randColor].toString());
    

    以下是我的活动的完整代码:

    public class Stroop extends ActionBarActivity {
    
        HashMap<String, Integer> colors = new HashMap<>();
        // putting the strings and color vals of the hashmap to an array
        Object stringOnScreen[];
        Object colorsOnScreen[];
    
        // declare vars
        TextView color;
        Button btn1;
        Button btn2;
        TextView result;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.stroop);
    
            setUpGame();
    
            stringOnScreen = colors.keySet().toArray();
            colorsOnScreen = colors.values().toArray();
    
            setUpQuestion();
    
            Log.d("Length", "Length string: " + stringOnScreen.length);
    
            Log.d("Length", "Length color: " + colorsOnScreen.length);
    
        }// oncreate end
    
        public void setUpQuestion() {
    
            int randString = new Random().nextInt(stringOnScreen.length);
            int randColor = new Random().nextInt(colorsOnScreen.length);
    
            Log.d("ranString", "randString: " + randString);
            Log.d("rancolor", "randcolor: " + randColor);
    
            // set the text of the string in textview for user to see
            color.setText("" + stringOnScreen[randString]);
            color.setTextColor((int) colorsOnScreen[randColor]);
    
            btn1.setText("" + stringOnScreen[randString]); //Set btn1 to the string val
    
            btn2.setText("" + colorsOnScreen[randColor].toString()); // set btn2 to the color of the String
    
    
        }
    
        public void setUpGame() {
    
            // setting up the hashmap
            colors.put("Green", Color.GREEN);
            colors.put("Blue", Color.BLUE);
            colors.put("Red", Color.RED);
            colors.put("Yellow", Color.YELLOW);
            colors.put("Black", Color.BLACK);
    
            // setting up vars
            color = (TextView) findViewById(R.id.tvStroopColor);
            btn1 = (Button) findViewById(R.id.btnStroop1);
            btn2 = (Button) findViewById(R.id.btnStroop2);
            result = (TextView) findViewById(R.id.tvStroopResults);
    
        }
    
    }
    

    编辑:

    尝试将按钮2中的字符串值与textView文本的颜色进行比较失败:

    if(btn2.getText().equals(color.getCurrentTextColor())){
    
                result.setText("Correct");
            }
    

1 个答案:

答案 0 :(得分:0)

做类似的事情:

public void setBtn2Text(){
    switch(color.getCurrentTextColor()){
        case Color.GREEN:
            btn2.setText("Green");
        case Color.RED:
            btn2.setText("Red");
            break;
        // Continue for the other colors
    }
}

然后在setBtn2Text();

中致电setupQuestion();后致电onCreate()

修改

将按钮文本字符串与文本视图文本颜色进行比较

public boolean checkForMatch(Button btn2){
    if(color.getCurrentTextColor() == Color.GREEN && btn2.getText().equals("Green"))
        return true;
    else if(color.getCurrentTextColor() == Color.RED && btn2.getText().equals("Red"))
        return true;
    // continue for the rest of the colors
    else 
        return false;
}

并做

public void onClick(View v){
    if(v.getId() == btn2.getId()){
        if(checkForMatch(btn2))
            result.setText("Correct!");
        else
            result.setText("Wrong!");
    }

    // Do what you need to for your other buttons
}

当然还有其他方法可以做到这一点(例如在onClick中使用开关),但这是一个简单方法的例子。