来自 public class 的 AlertDialogs

AlertDialogs from a public class

我正在查看我在 2014 年编写的 Android 应用程序,并将 Dialogs 替换为 AlertDialogs

以前,我一直在使用以下对话框代码来创建一个弹出窗口(来自此弹出窗口),该弹出窗口将出现在“清除Table”时出现,并会警告用户的结果。

public class locationPopupWindow {
Context ctx;
Button btnDismiss, btnSaveRecord, btnPurgeTable, btnUpdateRecord, btnDeleteRecord, btnClearForm, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;

public locationPopupWindow(Context ctx){
    this.ctx = ctx;
}
public void init(){
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popUpView = inflater.inflate(R.layout.location_popup_layout, null, false);
    final PopupWindow popup = new PopupWindow(popUpView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    popup.setContentView(popUpView);

...     
    
    btnPurgeTable=(Button)popUpView.findViewById(R.id.btnPurgeTablexml);
    btnPurgeTable.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            vibrate(100);
            openRedDialog();
        }
    });
}

private void openRedDialog() { 
  final Dialog redDialog = new Dialog(this.ctx,android.R.style.Theme_Translucent);
  redDialog.setCancelable(true);
  redDialog.setContentView(R.layout.red_dialog);

  TextView tvRedDialogBody = (TextView)redDialog.findViewById(R.id.tvRedDialogtitle);
  tvRedDialogBody.setTypeface(null, Typeface.BOLD);
  tvRedDialogBody.setText(" Are you really sure you want to permanently delete your data???");

  Button btnDeleteTable = (Button) redDialog.findViewById(R.id.btndeletetable);
  Button btnCancel = (Button)redDialog.findViewById(R.id.btncanceldelete);
  btnDeleteTable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            vibrate(100);
            purgeTable();
            redDialog.dismiss();
            Toast.makeText(ctx.getApplicationContext(), "Lookup table was purged. Please add new values.", Toast.LENGTH_LONG).show();
        }
    });
    btnCancel.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            vibrate(100);
            redDialog.dismiss();
        }
    });
    new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
        }
        @Override
        public void onFinish() {
            redDialog.dismiss();
        }
    }.start();
    redDialog.show();
   }
...
}

我已经交换了以下 openRedDialog() 的 AlertDialog 实现,

   private void openRedDialog(Context context) {//uses context
    AlertDialog openRedDialog = new AlertDialog.Builder(context).create(); 
    openRedDialog.setTitle("Warning");
    openRedDialog.setMessage("This will delete the whole table");
    openRedDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    openRedDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete Table",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    purgeTable();
                    dialog.dismiss();
                }
            });
    openRedDialog.show();
}

效果很好。

问题:当我像上面那样实现 AlertDialog 时,我如何膨胀我的 .xml 资源? 提前致谢...

添加这个,

LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.red_dialog, null);

得到这个,

   private void openRedDialog(Context context) {
    AlertDialog openRedDialog = new AlertDialog.Builder(context).create();
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.red_dialog, null);

在视图中包含 xml。