2017-04-23 89 views
0

虽然在按下中性按钮时没有提到关闭或关闭对话框,但我的应用程序仍然觉得按下时需要关闭对话框。AlertDialog中性按钮消除对话框

任何想法为什么?

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

问题并不清楚......你是否想在中性按钮按下时关闭对话框? – rafsanahmad007

+0

@ rafsanahmad007不,我不想关闭 – Sean

回答

1

原来需要一个OnShowListener,它必须在其中定义onClickListener。如果在使用DialogBu​​ilder时尝试定义中性按钮功能,或者在创建对话框(并显示对话框之前)后设置按钮功能,这将不起作用。

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()); 
        } 
       } 
      }); 
     } 
    }); 
+0

一个工作的解决方法。除非在显示对话框为空之前指定回调,否则中性按钮和负按钮都会自动解除。 –

0

在AlertDialog每个按钮会关闭它 只有当你这样做:

mAlertDialog.setView(mButton); 

一个mButton点击不会关闭它

+0

setView()不是用来设置对话框的布局吗? – Sean

+0

它是,所以如果你想要执行点击AlertDialog w/o关闭它就是这样做 –

0

所有AlertDialog按钮将其关闭。如果你需要一个按钮,并且你想让AlertDialog没有关闭,你将不得不使用一个自定义的视图。

相关问题