通过使用处理程序更新图像视图-如何正确使用条件语句



我正在尝试创建一个简单的莫尔斯电码应用程序。我想要一个图像视图根据给定的莫尔斯电码序列改变颜色。我尝试使用一个处理程序来更新UI线程中的图像视图。

问题是,即使我可以返回消息,我也不确定如何使用返回的信息来更改图像视图的颜色。我不确定我是否正确地做到了这一点(这对我来说都是新的),但我已经创建了一个捆绑包,并将其发送到UI-我需要使用这些信息来确定图像视图应该是什么颜色。我想知道是否有人对此有任何想法/建议?我正处于知识的边缘,并试图研究这个话题。

  //Handler which receives the message
  static Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
         Bundle bundle = msg.getData();
         String str1 = bundle.getString("dot");
         String str2 = bundle.getString("dash");
  //I think i need to use a conditional statement here
    }
 }; 

  //Logic done within the worker thread
 for (int i = 0; i < cArray.length; i++) {
        if (cArray[i] == '.') {
            msg = new Message();
            bundle = new Bundle();
            bundle.putString("dot", "dot dot dot");
            msg.setData(bundle);
            Sos.myHandler.sendMessage(msg);
            try {
                Thread.sleep(50);
            }catch(Exception e)
            {
            }
            try {
                Thread.sleep(offIntervalTime);
            }catch(Exception e)
            {
            }
        } else if(cArray[i] == ' ')
        {
            try{
                Thread.sleep(100);
            }catch(Exception e)
            {
            }
        }
        else {
            msg = new Message();
            bundle = new Bundle();
            bundle.putString("dash", "dash dash dash");
            msg.setData(bundle);
            Sos.myHandler.sendMessage(msg);
            try{
                Thread.sleep(dash);
            }catch(Exception e)
            {
            }
            try{
                Thread.sleep(offIntervalTime);
            }catch(Exception e)
            {
            }
        }
    }

由于我的建议对您有效,请将其添加为答案。

使用msg.what值来设置/get并基于该值执行操作。

最新更新