开关盒不能正常工作 [英] switch-case does not work properly
问题描述
我在下面发布的代码,当我离开字段 ip
空白/空并给其他字段赋值时, toast
总是给消息 KATimer无效或缺失
。
i the below posted code, when I leave the field ip
blank/empty and give values to the other fields, the toast
always gives message the KATimer is invalid or missing
.
我希望看到 toast
显示一条消息,指示空字段,但下面的代码,如果有任何字段为空,则总是显示 KATimer无效或为空
。
i expected to see a toast
showing with a message indicating the empty field,but the below code, if any field is empty, it always says KATimer is invalid or empty
.
为什么会发生这种情况,我错过了什么
why that is happeneing, i am missing something
代码:
btnStubView_Connect :
btnStubView_Connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isValidMQTTConfigs(etStubView_ip) &&
isValidMQTTConfigs(etStubView_port) &&
isValidMQTTConfigs(etStubView_ClientID) &&
isValidMQTTConfigs(etStubView_KATimer)) {
Log.d(TAG, "@btnStubView_ConnectListener(): all entries are valid");
setCSession(cbStubView_CS.isChecked()); // set the current state of the cleanSession checkBox.
addToContentValues();
Log.d(TAG, "@btnStubView_ConnectListener(): all entries added toContentValues");
} else {
Log.w(TAG, "@btnStubView_ConnectListener(): one or more entry(s) is invalid or left blank.");
}
}
});
isValidMQTTConfigs :
protected boolean isValidMQTTConfigs(View view) {
// TODO Auto-generated method stub
boolean valid = false;
String viewName = "";
switch(view.getId()) {
case R.id.etSubView_ip:
viewName = "IP";
if (isDuly( ((EditText) view).getText().toString())) {
this.setIP(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_port:
viewName = "Port";
if (isDuly( ((EditText) view).getText().toString())) {
this.setPort(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_clientID:
viewName = "clientID";
if (isDuly( ((EditText) view).getText().toString())) {
this.setClienID(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_KATimer:
viewName = "KAtimer";
if (isDuly( ((EditText) view).getText().toString())) {
this.setKATimer(((EditText) view).getText().toString());
return valid = true;
}
}
Log.w(TAG, "@checkMQTTConfigs(): " + viewName + " is invalid or missing");
Toast.makeText(getActivity(), viewName + " is invalid or missing", Toast.LENGTH_SHORT).show();
return valid;
}
isDuly :
private boolean isDuly(String text) {
// TODO Auto-generated method stub
if (text.trim().equals("")) {
return false;
} else {
return true;
}
}
推荐答案
你似乎缺少几个break语句。
You seem to be missing several break statements.
Switch(X){
case 1: doOne();
case 2: doTwo();
case 3: doThree();
}
瀑布流确保如果X为2,则doTwo和doThree都将被执行。如果X为1,则将执行所有三种方法。
the waterfall flow makes sure that if X is 2, both doTwo and doThree will be executed. If X is 1, all three methods will be executed.
如果只想调用链接方法,请将代码更改为:
If you want only the linked method to be called, change the code into:
Switch(X){
case 1: doOne(); break;
case 2: doTwo(); break;
case 3: doThree(); break;
}
这篇关于开关盒不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!