2016-12-03 93 views
0

構建器。我想在解僱或取消時執行一些操作。所以我使用setOnDismissListenersetOnCancelListener就像下面的代碼。但是這兩種方法都不能檢測何時解僱AlertDialog.BuilderAlertDialog.Builder解僱不適用於API 17 android

private static void openOptionDialog(final Context context, Item[] items, String title, DialogInterface.OnClickListener positiveClick) { 
     ListAdapter adapter = new ArrayAdapter<Item>(
       context, android.R.layout.select_dialog_item, items) { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View view = super.getView(position, convertView, parent); 
       TextView textView = (TextView) view.findViewById(android.R.id.text1); 
       Item item = getItem(position); 
       textView.setText(item.string); 
       textView.setTextSize(16f); 
       textView.setCompoundDrawablesWithIntrinsicBounds(item.icon, 0, 0, 0); 
       textView.setCompoundDrawablePadding(BDevice.getPixelFromDp(context, 15)); 
       return view; 
      } 
     }; 

     AlertDialog.Builder builder = AlertUtils.getBuilder(context); 
     builder.setTitle(title); 
     builder.setAdapter(adapter, positiveClick); 
     builder.create().show(); 

     builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 
      @Override 
      public void onDismiss(DialogInterface dialog) { 
       WidgetSettings preference = new WidgetSettings(context); 
       if(preference.canWidgetTooteets()){ 
        preference.clear(); 
       } 
      } 
     }); 

     builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 

      public void onCancel(DialogInterface dialog) { 
       WidgetSettings preference = new WidgetSettings(context); 
       if(preference.canWidgetTooteets()){ 
        preference.clear(); 
       } 
      } 
     }); 

    } 

AlertUtilsgetBuilder方法如下,

public static AlertDialog.Builder getBuilder(Context context) { 
     return new AlertDialog.Builder(context, R.style.Dialog); 
    } 

可否請你建議我找到了一個解決方案?提前致謝。

+0

ü要使用Aleart或像Dismis或OK aleart按鈕進行操作? –

+0

@RavishSharma我想在解散AlertDialog.Builder時執行一些操作。 – Sangeetha

+1

@Sangeetha嘗試* AlertDialog *而不是* AlertDialog.Builder *並添加* setOnDismissListener *,因爲它可以工作。 –

回答

0

嘗試用AlertDialog改爲只AlertDialog.Builder和它會成功添加setOnDismissListener

例如,

AlertDialog alert = builder.create(); 
alert.setOnDismissListener(yourDismissListener); 
alert.show(); 

AlertDialog alert = builder.create(); 
alert.setOnCancelListener(yourCancelListener); 
alert.show(); 
0

嘗試alert.cancel()代替alert.dismiss()並添加OnCancelListener這樣的:

builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 
     @Override 
     public void onCancel(DialogInterface dialog) { 
      dialog.dismiss(); 
     } 
    }); 
相關問題