使用线程更改片段内按钮的颜色

时间:2014-06-06 11:15:03

标签: android android-fragments

我有一个包含片段的活动,它连接到服务器。当服务器发送消息时,我想更改当前片段的按钮颜色。我试图在我的片段中使用Runnable类,但它不起作用。我怎么能这样做?

这是我片段的代码:

public class changeBtn implements Runnable
{
    public String[] array;
    public changeBtn(String[] _array)
    {
        this.array = _array;
    }
    @Override
    public void run() {
        acikMasalar=array;
        for (int i = 0;i<masalar.size();i++)
        {
            button = (Button) fragmentView.findViewWithTag(masalar.get(i));
            button.setBackgroundColor(Color.RED);
        }
    }
}

    public void startProg(String[] array)
    {
        new Thread(new changeBtn(array)).start();
    }

这是在活动中运行的tcp客户端代码:

public class connectTask extends AsyncTask<String,String,TCPClient> {

        @Override
        protected TCPClient doInBackground(String... message) {

            //we create a TCPClient object and
            mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
                @Override
                //here the messageReceived method is implemented
                public void messageReceived(String message) {
                    //this method calls the onProgressUpdate
                    srvrMessage = message;
                    if(srvrMessage!=null&&message!=null) {
                        acikMasalar = srvrMessage.split("\\*");
                        FragmentMasaDesing fragment = (FragmentMasaDesing) getSupportFragmentManager()
                                .getFragments().get(1);
                        if(fragment!=null) {
//                            fragment.changeButtonColor();
                            fragment.startProg(acikMasalar);
                        }
                    }
                }
            });
            mTcpClient.run();

            return null;
        }
    }

0 个答案:

没有答案