2017-04-23 43 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沒有關閉,你將不得不使用一個自定義的視圖。

相關問題