[英]AlertDialog neutral button dismissing the dialog
虽然没有提到在按下空档按钮时关闭或关闭我的对话框,但我的应用仍然感觉需要在按下时关闭对话框。
有什么想法吗?
dialogBox = (AlertDialog) dialogBoxHandler.locationDialog();
dialogBox.setButton(DialogInterface.BUTTON_NEUTRAL, "Use Current Location", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText latitude = (EditText) dialogBox.findViewById(R.id.dl_et_latitude);
EditText longitude = (EditText) dialogBox.findViewById(R.id.dl_et_longitude);
LocationManager lm = (LocationManager) MessageSelection.this.getSystemService(Context.LOCATION_SERVICE);
try {
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double currentLongitude = location.getLongitude();
double currentLatitude = location.getLatitude();
latitude.setText(Double.toString(currentLatitude));
longitude.setText(Double.toString(currentLongitude));
Log.d(TAG, "Latitude " + currentLatitude + " Longitude " + currentLongitude);
} catch (SecurityException e){
Log.d(TAG, e.toString());
}
}
});
dialogBox.show();
结果是需要一个OnShowListener,它必须在其中定义onClickListener。 当在使用DialogBuilder时尝试定义中性按钮功能或在创建对话框之后(以及在显示对话框之前)设置按钮功能时,这将不起作用。
dialogBuilder.setView(inflater.inflate(R.layout.dialog_location, null))
.setNeutralButton("Use Current Location", null);
final AlertDialog locationDialog = dialogBuilder.create();
locationDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText latitude = (EditText) locationDialog.findViewById(R.id.dl_et_latitude);
EditText longitude = (EditText) locationDialog.findViewById(R.id.dl_et_longitude);
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double currentLongitude = location.getLongitude();
double currentLatitude = location.getLatitude();
latitude.setText(Double.toString(currentLatitude));
longitude.setText(Double.toString(currentLongitude));
Log.d(TAG, "Latitude " + currentLatitude + " Longitude " + currentLongitude);
} catch (SecurityException e){
Log.d(TAG, e.toString());
}
}
});
}
});
AlertDialog中的每个按钮仅在您执行以下操作时才会将其解除:
mAlertDialog.setView(mButton);
mButton点击不会解雇它
所有AlertDialog按钮都将关闭它。 如果您需要一个按钮并且希望AlertDialog不被忽略,则必须使用内部按钮的自定义视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.