我已经在两个编辑文本上实现了设置错误的代码。
- 首先获取任何没有 null 或空值的字符串。
- 其次要获取任何没有 null 或空值的字符串,并且必须有一些 Double 值。
代码如下
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SearchPlaces extends Activity {
EditText place;
EditText distance;
Button search;
static String _place;
static String _distance;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
place = (EditText) findViewById(R.id.place);
distance = (EditText) findViewById(R.id.distance);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Map<String, String> validate = new HashMap<String, String>();
_place = place.getText().toString();
_distance = distance.getText().toString();
validate.put("placename", _place);
validate.put("distancenumeric", _distance);
if (!validateInputInformation(validate)) {
return;
}
if ((_place == "" && _distance == "")) {
Toast.makeText(getApplicationContext(),
"Please fill all the information",
Toast.LENGTH_SHORT).show();
} else {
_place = place.getText().toString();
_distance = distance.getText().toString();
Intent placedist = new Intent(getApplicationContext(),
MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("place", _place);
bundle.putString("distance", _distance);
placedist.putExtras(bundle);
startActivity(placedist);
}
}
private boolean validateInputInformation(
Map<String, String> validate) {
String l_place = validate.get("placename");
if (l_place == null || l_place.equals("")) {
place.setError("Please enter the Place First");
return false;
}
String l_distance = validate.get("distancenumeric");
if (l_distance == null || l_distance.equals("")) {
distance.setError("Please enter the Distance First");
return false;
}
if (!isDoubleNumber(l_distance)) {
distance.setError("Please enter Numeric Value");
return false;
}
return true;
}
private boolean isDoubleNumber (String num) {
try {
Double.parseDouble(num);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
});
}
}
实施后,所有工作都很好,但是当我没有输入并单击搜索按钮时,错误会在第一个编辑框中弹出,然后当我根据需要重新输入正确的值时,错误通知不会消失,但搜索按钮效果很好。
请帮助我,以便可以从第一个编辑框中删除设置错误通知。