我在下面發佈的代碼,當我離開字段ip
空白/空值並給其他字段值時,toast
總是給出信息KATimer is invalid or missing
。開關盒不能正常工作
我預計看到一個toast
顯示消息指示空字段,但下面的代碼,如果任何字段爲空,它總是說KATimer is invalid or empty
。
這是爲什麼happeneing,我失去了一些東西
代碼:
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;
}
}
哪裏是打破; – PPD
不是一個好問題。使用中斷 –
switch-case **確實工作正常** ...如果您知道如何使用它! –