异步任务更新按钮 [英] Asynctask for update button

查看:43
本文介绍了异步任务更新按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有条件,如何为禁用的按钮开/关更新UI按钮.条件:如果我得到的值= 1.0,则按钮的电源将被禁用&;如果我的值为0,则按钮电源已禁用.我希望按钮始终显示新值.

How to update my UI button for disabled button on/off if i have condition. The condition : if i get value = 1.0 the button power on is disabled & and if i get value 0 the button power off disabled. I want the button always show up the new value.

这是我的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_controller, container, false);
        mSwitchStatus  = (TextView) rootView.findViewById(R.id.statusSwitch);
        ImageButton On = (ImageButton)rootView.findViewById(R.id.on);
        ImageButton Off = (ImageButton)rootView.findViewById(R.id.off);


        callAsynchronousTask();

            On.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Toast.makeText(getActivity().getApplication(), "ON", Toast.LENGTH_SHORT).show();
                    SendApi sendApi = new SendApi();
                    sendApi.execute();
                }
            });

            Off.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    Toast.makeText(getActivity().getApplication(), "OFF", Toast.LENGTH_SHORT).show();
                    SendApi sendApi = new SendApi();
                    sendApi.execute();
                }
            });
        // Inflate the layout for this fragment
        return rootView;
    }

重复AsyncTask

Repeat AsyncTask

public void callAsynchronousTask() {
        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            GetApi getApi = new GetApi();
                            getApi.execute();

                        } catch (Exception e) {
                            android.util.Log.i("Error", "Error");
                            // TODO Auto-generated catch block
                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 100);
    }

用于GETApi的AsyncTask

AsyncTask for GETApi

public class GetApi extends AsyncTask<Object, Object, Value[]> {
        private final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
        private final String SWITCHID = "xxxxxxxxxxxxxxxxxxxxxxxxx"; 
        private ImageButton On,Off ;

        @Override
        protected Value[] doInBackground(Object... params) {
            ApiClient apiClient = new ApiClient(API_KEY);
            Variable statusSwitch = apiClient.getVariable(SWITCHID);
            Value[] variableValues = statusSwitch.getValues();

            return variableValues;
        }
        @Override
        protected void onPostExecute(Value[] variableValues) {

            String status = Double.toString(variableValues[0].getValue());
            mSwitchStatus.setText(status);

            if(status.equals("1.0")){
                On.setVisibility(View.INVISIBLE);
            }

        }
    }

Fragment_controller.xml

Fragment_controller.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#1d4851"
    tools:context=".activity.ControllerFragment">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/off"
        android:src="@drawable/ic_off"
        android:background="@null"
        android:layout_marginTop="79dp"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/on"
        android:layout_alignStart="@+id/on"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/on"
        android:background="@null"
        android:src="@drawable/ic_on"
        android:layout_marginTop="87dp"
        android:layout_alignTop="@+id/off"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Water Pump Controller "
        android:id="@+id/textView2"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:layout_marginTop="19dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="  "
        android:id="@+id/statusSwitch"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:layout_marginTop="250dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

推荐答案

您在onCreateView中的按钮initialize就像

ImageButton On = (ImageButton)rootView.findViewById(R.id.on); 

并尝试在onPostExecute()中访问它.它们不是全局的.

and trying to access it in onPostExecute().they aren't global.

要禁用此功能,您必须致电setEnable(false)而不是setVisibility

And to disable you have to call setEnable(false) not setVisibility

您也可以按照 Rathna Kumaran 所说的发送.

And you can also send it as Rathna Kumaran said.

public class GetApi extends AsyncTask<Object, Object, Value[]> {
        private Button button;
        public GetApi(Button mButton){
            this.button = button;
        }
        private final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
        private final String SWITCHID = "xxxxxxxxxxxxxxxxxxxxxxxxx";

        @Override
        protected Value[] doInBackground(Object... params) {
            ApiClient apiClient = new ApiClient(API_KEY);
            Variable statusSwitch = apiClient.getVariable(SWITCHID);
            Value[] variableValues = statusSwitch.getValues();

            return variableValues;
        }
        @Override
        protected void onPostExecute(Value[] variableValues) {

            String status = Double.toString(variableValues[0].getValue());
            mSwitchStatus.setText(status);

            if(status.equals("1.0")){
                button.setVisibility(View.INVISIBLE);
            }

        }
    }

这篇关于异步任务更新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆