微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自公共类的 AlertDialogs

如何解决来自公共类的 AlertDialogs

我正在浏览我在 2014 年编写的一个 Android 应用程序,并且正在用 AlertDialogs 替换 Dialogs

以前,我一直使用以下对话框代码创建一个弹出窗口(从此弹出窗口),该弹出窗口将在选择“清除表”时出现,并警告用户他们的操作结果。

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,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",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。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。