如何从对话框中的 Main Activity 更新 ImageButton 的图像资源
How update Image Resource for ImageButton from Main Activity in Dialog
我有 Main Activity 和 activity_main.xml,Dialog 有 activity_room.xml。我通过蓝牙从 Arduino 接收数据,当接收到字符串“1”或“0”时,我想编辑 ImageButton 图像资源。我把接收字符串写入变量,然后在Dialog里面if else构造检查变量,if 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;
}
}
};
你可以在 UI 线程上设置图像更新吗?
runOnUiThread(new Runnable() {
public void run() {
// changes image here.
}
});
并在可能的情况下共享代码行以更改图像。
已修复。我将此代码添加到 OnCreate 方法
dialogRoom1 = new Dialog(this);
dialogRoom1.setContentView(R.layout.activity_room);
ImageButton btnOnOffVent = dialogRoom1.findViewById(R.id.btnOnOffVent);
创建 Public 对话框方法。
一切都很完美。 HiteshGs 非常感谢。
我有 Main Activity 和 activity_main.xml,Dialog 有 activity_room.xml。我通过蓝牙从 Arduino 接收数据,当接收到字符串“1”或“0”时,我想编辑 ImageButton 图像资源。我把接收字符串写入变量,然后在Dialog里面if else构造检查变量,if 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;
}
}
};
你可以在 UI 线程上设置图像更新吗?
runOnUiThread(new Runnable() {
public void run() {
// changes image here.
}
});
并在可能的情况下共享代码行以更改图像。
已修复。我将此代码添加到 OnCreate 方法
dialogRoom1 = new Dialog(this);
dialogRoom1.setContentView(R.layout.activity_room);
ImageButton btnOnOffVent = dialogRoom1.findViewById(R.id.btnOnOffVent);
创建 Public 对话框方法。
一切都很完美。 HiteshGs 非常感谢。