虽然在按下空档按钮时没有提及关闭或关闭我的对话框,但我的应用程序仍然感觉需要在按下时关闭对话框。
任何想法为什么?
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();
答案 0 :(得分:3)
原来需要一个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());
}
}
});
}
});
答案 1 :(得分:0)
AlertDialog中的每个按钮都会将其关闭 只有你这样做:
mAlertDialog.setView(mButton);
mButton点击不会解雇它
答案 2 :(得分:0)
所有AlertDialog按钮都会将其关闭。如果您需要一个按钮并且希望AlertDialog不被忽略,则必须使用内部按钮的自定义视图。