仅当在两个positivebutton
字段中都设置了文本时,我才尝试启用AlertDialog
的editText
,但这是行不通的,即,即使我在两个字段中都有文本也都不会显示PositiveButton
。
当然,如果我在两个字段中都没有文本,则会出现错误NumberFormatException: empty String
case R.id.action_geolocate:
AlertDialog.Builder placePicker = new AlertDialog.Builder(this);
LayoutInflater li = LayoutInflater.from(this);
final View view = li.inflate(R.layout.fragment_place, null);
placePicker.setView(view);
placePicker.setTitle("Enter Latitude and Longitude");
final EditText lat2 = view.findViewById(R.id.lati2);
final EditText long2 = view.findViewById(R.id.longi2);
if (lat2.getText().toString().length()>0 && long2.getText().toString().length()>0) {
placePicker.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Lat = Double.parseDouble(lat2.getText().toString());
Long = Double.parseDouble(long2.getText().toString());
setupViewPager();
}
});
}
placePicker.setNegativeButton("Cancel", null);
placePicker.show();
return true;
在检查String
的长度之前,请确认它们不是null
:
if ((lat2.getText()!=null) && (long2.getText()!=null)) {
if (lat2.getText().toString().length()>0 && long2.getText().toString().length()>0) {...
首先禁用对话框的肯定按钮,如下所示:
placePicker.setNegativeButton("Cancel", null); AlertDialog dialog = placePicker.create(); dialog.show(); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
稍后,您可以使用TextWatcher获取EditText的状态。请遵循此link以更好地了解TextWatcher。对于崩溃,您可以应用此修复程序:
if (!TextUtils.isEmpty(lat2.getText().toString()) && !TextUtils.isEmpty(long2.getText().toString())) {
Lat = Double.parseDouble(lat2.getText().toString());
Long = Double.parseDouble(long2.getText().toString());
}