1. 程式人生 > >android alertdialog不能彈出彈窗問題

android alertdialog不能彈出彈窗問題

很多人會遇到這樣一個問題,就是在使用alertdialog的時候並不能很理想的彈出彈窗,看了程式碼也找不出什麼問題, 這個時候很可能就是在new alertdialog.build(acivity)這個方法裡面的引數有問題,很多人喜歡使用getapplaication或和獲取application引用的context, 這樣是不對的, 我們可以仔細的想一下就會知道問題所在, 彈窗是依賴於所在的activity的,所以, 這個引數就必須是所依賴的activity的引用;程式碼如下:

AlertDialog.Builder builder = new AlertDialog.builder(mActivity);//注意這裡的引數要是dialog依賴的acitivity的引用;

builder.setView(View.inflate(mactivity, R.layout.bkg_layout,null));

AlertDialog dialog = builder.create();

dialog.setCancable(false);

// 中間省略一萬字,呵呵噠

dialog.show();

備註: 本人小白一枚,第一次寫部落格,不喜勿噴!