亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 android 中同一類中的任何其他方法取消/關閉 alertdialog 生成器?

從 android 中同一類中的任何其他方法取消/關閉 alertdialog 生成器?

開滿天機 2023-02-23 16:53:45
我有一個 java 類沒有活動。在任何其他活動中,我都調用了此類,并在該類中創建了 alertdialog 構建器。在那里我膨脹了來自數據庫的數據?,F在在這個類中,我還有其他偵聽器和方法。在其中一種方法中,我想關閉/取消此對話框。就像我們怎么做setResult(RESULT_OK, intent);        finish();在任何活動中,我想在課堂上做同樣的事情。代碼:我從活動中調用的這個方法。 public void showProvidersDialog(long customCategoryId) {        categoryId = customCategoryId;        LayoutInflater li = LayoutInflater.from(context);        promptsView = li.inflate(R.layout.row_providers_layout, null);        init();        alertDialogBuilder = new android.app.AlertDialog.Builder(context, R.style.dialogBoxStyle);        alertDialogBuilder.setView(promptsView);        alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                dialog.dismiss();            }        });        isInsurance();        alertDialogBuilder.show();//solved:  dialog = alertDialogBuilder.create();        dialog.show();        }我在同一個 java 類中還有一個方法,我想從那個方法中關閉當前打開的對話框。  private void sendProviderData(General provider) {        Singleton.getInstance().setProviderId(provider.getId());        Singleton.getInstance().setProviderIcon(provider.getIcon());        Singleton.getInstance().setProviderName(provider.getName());//solveddialog.dismiss}再次說明:看,我可以取消否定按鈕內的對話框。但我想要的是,在那個對話框中,我膨脹了包含一個聯系人列表的行。我希望當用戶點擊任何聯系人時(比如說點擊觸摸監聽器上的回收站)我正在使用單例傳遞一些數據,同時我想關閉對話框。
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

這是通用目的的對話代碼。您可以在需要顯示對話框時隨時調用。您可以通過調用方法顯示對話框showDialog()并通過調用dismissDialog()方法關閉。


/*

* call whenever dialog is required in whole app in form of popup

*/

public class MyDialog implements View.OnClickListener {

  private Dialog dialog;

  private Context context;

  private TextView tvTitle;

  private TextView tvSubtitle;

  private Button bt_ok;

  private String strInvalidUserNamePass, strHeader;

  /*

    * constructor to change the text dynamically.

  */

  public MyDialog(Context context, String strHeader, String invalidUserNamePass) {

     this.context = context;

     this.strInvalidUserNamePass = invalidUserNamePass;

     this.strHeader = strHeader;

     if (context != null) {

         initDialog();

     }

 }

 /*

  * set id of all the view components and implement listeners

  */

 private void initDialog() {


    dialog = new Dialog(context, R.style.FMDialogNormal);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.setContentView(R.layout.dialog_validation);

    dialog.setCancelable(false);

    dialog.setCanceledOnTouchOutside(false);

    dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

    dialog.show();


    tvTitle = (TextView) dialog.findViewById(R.id.tv_title);

    tvSubtitle = (TextView) dialog.findViewById(R.id.tv_subtitle);

    tvTitle.setText(strHeader);

    tvSubtitle.setText(strInvalidUserNamePass);

    bt_ok = (Button) dialog.findViewById(R.id.bt_ok);

    bt_ok.setOnClickListener(this);


}

/*

 * Implement listener according to the views

 */

 @Override

 public void onClick(View view) {

     switch (view.getId()) {

         case R.id.bt_ok:

             dialog.dismiss();

             break;

     }

 }

 public void showDialog(){

     if(dialog!=null){

         dialog.show();

     }

 }

 public void dismissDialog(){

     if(dialog!=null && isVisible()){

         dialog.show();

     }

 }  

 public boolean isVisible() {

     if (dialog != null) {

         return dialog.isShowing();

     }

     return false;

   }

 }



查看完整回答
反對 回復 2023-02-23
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號