如何从对话框中的主活动更新 ImageButton 的图像资源
Posted
技术标签:
【中文标题】如何从对话框中的主活动更新 ImageButton 的图像资源【英文标题】:How update Image Resource for ImageButton from Main Activity in Dialog 【发布时间】:2021-07-04 00:08:34 【问题描述】:我有带有activity_main.xml 的主要活动,带有activity_room.xml 的对话框。我通过蓝牙从 Arduino 接收数据,我想在接收字符串“1”或“0”时编辑 ImageButton 图像资源。我将接收字符串写入变量,然后在对话框中 if else 构造检查变量,如果 1 ImageResource ON else OFF。
问题。对话框打开时我无法更新图像资源。为了更新图像资源,我需要重新打开对话框
更新。
我可以用 Kludge 来解决我的问题,如果还有很多,以及 arduino 的附加代码。
对话代码。
当 btnStateOnOff = 1 系统开启时。当 btnStateOnOff = 0 系统关闭且 btnStateVent = 0 时,在 adruino 中,如果系统关闭,btnStateVent = 2,用于 ImageButton btnStateVentR1 的不可编辑图像资源。这是我的 Kludge。
public void DialogR1()
dialogRoom1 = new Dialog(this);
// dialogRoom1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogRoom1.setContentView(R.layout.activity_room);
dialogRoom1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
ImageButton btnOnOff = dialogRoom1.findViewById(R.id.btnOnOff);
ImageButton btnOnOffVent = dialogRoom1.findViewById(R.id.btnOnOffVent);
ImageButton btnOnOffKlimat = dialogRoom1.findViewById(R.id.btnOnOffKlimat);
TextView tvDialogR1 = (TextView) dialogRoom1.findViewById(R.id.tvDialog);
TextView tvTemp = dialogRoom1.findViewById(R.id.tvTemp);
TextView btnclose = (TextView) dialogRoom1.findViewById(R.id.btnClose);
EditText etWriteTempR1 = dialogRoom1.findViewById(R.id.etWriteTemp);
WindowManager.LayoutParams lp = dialogRoom1.getWindow().getAttributes();
lp.width = MATCH_PARENT;
lp.height = MATCH_PARENT;
dialogRoom1.getWindow().setAttributes(lp);
dialogRoom1.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
tvDialogR1.setText("Room 1");
dialogRoom1.setCancelable(true);
tvTemp.setText(temp1);
if (btnStateOnOff1 == 1)
btnOnOff.setImageResource(R.drawable.on);
else if (btnStateOnOff1 == 0)
btnOnOff.setImageResource(R.drawable.off);
if (btnStateVent1 == 1)
btnOnOffVent.setImageResource(R.drawable.on);
else if (btnStateVent1 == 0)
btnOnOffVent.setImageResource(R.drawable.off);
else if (btnStateVent1 == 2 )
btnOnOffVent.setImageResource(R.drawable.off);
if (btnStateOnOff1 == 1 && btnStateVent1 == 2)
btnStateVent1 = 0;
if (btnStateKlimat1 == 1)
btnOnOffKlimat.setImageResource(R.drawable.on);
else if (btnStateKlimat1 == 0)
btnOnOffKlimat.setImageResource(R.drawable.off);
btnOnOff.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (btnStateOnOff1 == 1)
connectedThread.write("onoffr1 0;");
connectedThread.write("ventr1 0;");
btnOnOff.setImageResource(R.drawable.off);
btnOnOffVent.setImageResource(R.drawable.off);
btnStateVent1 = 2;
else if (btnStateOnOff1 == 0)
connectedThread.write("onoffr1 1;");
btnOnOff.setImageResource(R.drawable.on);
btnStateVent1 = 0;
);
btnOnOffVent.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (btnStateVent1 == 1)
connectedThread.write("ventr1 0;");
btnOnOffVent.setImageResource(R.drawable.off);
else if (btnStateVent1 == 0)
connectedThread.write("ventr1 1;");
btnOnOffVent.setImageResource(R.drawable.on);
else if (btnStateVent1 == 2)
btnOnOffVent.setImageResource(R.drawable.off);
);
btnOnOffKlimat.setOnClickListener(new View.OnClickListener()
@Override
public void onClick (View v)
if (btnStateKlimat1 == 1)
connectedThread.write("klimat1 0;");
btnOnOffKlimat.setImageResource(R.drawable.off);
else if (btnStateKlimat1 == 0)
connectedThread.write("klimat1 1;");
btnOnOffKlimat.setImageResource(R.drawable.on);
);
btnclose.setOnClickListener(new View.OnClickListener()
@Override
public void onClick (View v)
dialogRoom1.dismiss();
);
etWriteTempR1.setOnKeyListener(new View.OnKeyListener()
@Override
public boolean onKey (View view,int i, KeyEvent keyEvent)
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN &&
(i == KeyEvent.KEYCODE_ENTER))
Log.e("SendText", "temp1 " + etWriteTempR1.getText().toString() + ";");
connectedThread.write("temp1 " + etWriteTempR1.getText().toString() + ";");
etWriteTempR1.setText("");
return true;
return false;
);
dialogRoom1.show();
蓝牙代码。
我从 arduino 接收字符串,收集字节到数组然后解析。
handler = new Handler(Looper.getMainLooper())
@Override
public void handleMessage(Message msg)
switch (msg.what)
case CONNECTING_STATUS:
switch (msg.arg1)
case 1:
//toolbar.setSubtitle("Connected to " + deviceName);
//progressBar.setVisibility(View.GONE);
btnBluetooth.setEnabled(true);
btnSend.setEnabled(true);
btnOnOffR1.setEnabled(true);
btnOnOffR2.setEnabled(true);
btnOnOffR3.setEnabled(true);
break;
case -1:
//toolbar.setSubtitle("Device fails to connect");
//progressBar.setVisibility(View.GONE);
btnBluetooth.setEnabled(true);
break;
break;
case MESSAGE_READ:
arduinoMsg = msg.obj.toString(); // Read message from Arduino
receiveArduino = arduinoMsg.split("/");
temp1 = receiveArduino[0];
temp2 = receiveArduino[1];
temp3 = receiveArduino[2];
klimat1 = receiveArduino[3];
klimat2 = receiveArduino[4];
klimat3 = receiveArduino[5];
onOffR1 = receiveArduino[6];
onOffR2 = receiveArduino[7];
onOffR3 = receiveArduino[8];
ventr1 = receiveArduino[9];
ventr2 = receiveArduino[10];
ventr3 = receiveArduino[11];
tvTemp1.setText(temp1);
tvTemp2.setText(temp2);
tvTemp3.setText(temp3);
if (onOffR1.equals("1"))
//THIS IS BUTTON PLACE IN ACTIVITY_MAIN.XML, MAIN LAYOUT
btnOnOffR1.setImageResource(R.drawable.on);
btnStateOnOff1 = 1;
else if (onOffR1.equals("0"))
//THIS IS BUTTON PLACE IN ACTIVITY_MAIN.XML, MAIN LAYOUT
btnOnOffR1.setImageResource(R.drawable.off);
btnStateOnOff1 = 0;
if (onOffR2.equals("1"))
//THIS IS BUTTON PLACE IN ACTIVITY_MAIN.XML, MAIN LAYOUT
btnOnOffR2.setImageResource(R.drawable.on);
btnStateOnOff2 = 1;
else if (onOffR2.equals("0"))
//THIS IS BUTTON PLACE IN ACTIVITY_MAIN.XML, MAIN LAYOUT
btnOnOffR2.setImageResource(R.drawable.off);
btnStateOnOff2 = 0;
if (onOffR3.equals("1"))
//THIS IS BUTTON PLACE IN ACTIVITY_MAIN.XML, MAIN LAYOUT
btnOnOffR3.setImageResource(R.drawable.on);
btnStateOnOff3 = 1;
else if (onOffR3.equals("0"))
//THIS IS BUTTON PLACE IN ACTIVITY_MAIN.XML, MAIN LAYOUT
btnOnOffR3.setImageResource(R.drawable.off);
btnStateOnOff3 = 0;
//vent
if (ventr1.equals("1"))
//btnOnOffVent.setImageResource(R.drawable.on);
//THIS IS GOOD SOLUTION FOR ME. THIS BUTTON PLACE IN ACTIVITY_ROOM.XML LAYOUT FOR DIALOG
btnStateVent1 = 1;
else if (ventr1.equals("0"))
//btnOnOffVent.setImageResource(R.drawable.off);
btnStateVent1 = 0;
else if (ventr1.equals("2"))
btnStateVent1 = 2;
if (ventr2.equals("1"))
btnStateVent2 = 1;
else if (ventr2.equals("0"))
btnStateVent2 = 0;
if (ventr3.equals("1"))
btnStateVent3 = 1;
else if (ventr3.equals("0"))
btnStateVent3 = 0;
//klimat
if (klimat1.equals("1"))
btnStateKlimat1 = 1;
else if (klimat1.equals("0"))
btnStateKlimat1 = 0;
if (klimat2.equals("1"))
btnStateKlimat2 = 1;
else if (klimat2.equals("0"))
btnStateKlimat2 = 0;
if (klimat3.equals("1"))
btnStateKlimat3 = 1;
else if (klimat3.equals("0"))
btnStateKlimat3 = 0;
if (btnStateOnOff1 == 1 && btnStateVent1 == 2)
btnStateVent1 = 0;
break;
;
【问题讨论】:
【参考方案1】:你能在 UI Thread 上设置图像更新吗?
runOnUiThread(new Runnable()
public void run()
// changes image here.
);
如果可能,还可以共享代码行来更改图像。
【讨论】:
我不明白你的意思。要在另一个布局中编辑视图,我需要调用布局。我怎样才能做到这一点?我编辑我的问题,添加代码。 你的蓝牙代码和Dialog在同一个activity中? 是的。在同一个主要活动中。但布局不同 好的,现在将 dialogRoom1 设为全局(在 Activity 范围内),然后在定义 dialogRoom1 上添加您想要更改的任何组件(如 btnOnOffR1),然后使用它。因为在您当前的代码中,我没有在 dialogRoom1 中找到 btnOnOffR1 的任何分配。【参考方案2】:已修复。我将此代码添加到 OnCreate 方法中
dialogRoom1 = new Dialog(this);
dialogRoom1.setContentView(R.layout.activity_room);
ImageButton btnOnOffVent = dialogRoom1.findViewById(R.id.btnOnOffVent);
创建公共对话框方法。
一切都很完美。 HiteshGs 非常感谢你。
【讨论】:
以上是关于如何从对话框中的主活动更新 ImageButton 的图像资源的主要内容,如果未能解决你的问题,请参考以下文章
从 onOptionsItemSelected 访问列表视图
在 Android 中的 ImageButton 中适合图像