gpt4 book ai didi

java - 更新按钮的异步任务

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:01 25 4
gpt4 key购买 nike

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

这是我的代码:

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;
}

重复异步任务

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 的异步任务

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

<?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初始化按钮,例如

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

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

要禁用,您必须调用 setEnable(false) 而不是 setVisibility

您也可以按照Rathna Kumaran所说的方式发送。

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);
}

}
}

关于java - 更新按钮的异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39867841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com