Android自定义AlertDialog
常見的一種方法:
[html]?view plaincopyprint?
AlertDialog.Builder?builder;??
????????????????AlertDialog?alertDialog;??
??
????????????????LayoutInflater?inflater?=?getLayoutInflater();??
????????????????//?添加自定義的布局文件??
????????????????View?layout?=?LayoutInflater.from(TestOne.this).inflate(??
????????????????????????R.layout.dialog,?null);??
????????????????final?TextView?text?=?(TextView)?layout.findViewById(R.id.tv1);??
????????????????//?添加點擊事件??
????????????????text.setOnClickListener(new?OnClickListener()?{??
??
????????????????????@Override??
????????????????????public?void?onClick(View?v)?{??
????????????????????????//?TODO?Auto-generated?method?stub??
????????????????????????text.setText("call");??
????????????????????}??
????????????????});??
??
????????????????builder?=?new?AlertDialog.Builder(TestOne.this);??
????????????????alertDialog?=?builder.create();??
????????????????//?去掉邊框的黑色,因為設(shè)置的與四周的間距為0??
????????????????alertDialog.setView(layout,?0,?0,?0,?0);??
????????????????alertDialog.show();??
????????????????//?修改大小??
????????????????WindowManager.LayoutParams?params?=?alertDialog.getWindow()??
????????????????????????.getAttributes();??
????????????????params.width?=?350;??
????????????????params.height?=?200;??
????????????????alertDialog.getWindow().setAttributes(params);??
這樣 ,重新給它填充自定義的布局視圖,但缺乏可擴(kuò)展性,而且每次使用還得重新定義。
?
?
重寫AlertDialog類,定義方法:
[html]?view plaincopyprint?
/**??
?*?自定義的對話框??
?*/??
public?abstract?class?MyAlerDialog?extends?AlertDialog?implements??
????????android.view.View.OnClickListener?{??
??
????protected?MyAlerDialog(Context?context)?{??
????????super(context);??
????????//?TODO?Auto-generated?constructor?stub??
????}??
??
????/**??
?????*?布局中的其中一個組件??
?????*/??
????private?TextView?txt;??
??
????@Override??
????protected?void?onCreate(Bundle?savedInstanceState)?{??
????????//?TODO?Auto-generated?method?stub??
????????super.onCreate(savedInstanceState);??
????????//?加載自定義布局??
????????setContentView(R.layout.dialog);??
????????//?setDialogSize(300,?200);??
????????txt?=?(TextView)?findViewById(R.id.tv1);??
????????txt.setOnClickListener(this);??
????}??
??
????/**??
?????*?修改?框體大小??
?????*???
?????*?@param?width??
?????*?@param?height??
?????*/??
????public?void?setDialogSize(int?width,?int?height)?{??
????????WindowManager.LayoutParams?params?=?getWindow().getAttributes();??
????????params.width?=?350;??
????????params.height?=?200;??
????????this.getWindow().setAttributes(params);??
????}??
??
????public?abstract?void?clickCallBack();??
??????
????/**??
?????*?點擊事件??
?????*/??
????@Override??
????public?void?onClick(View?v)?{??
????????//?TODO?Auto-generated?method?stub??
????????if?(v?==?txt)?{??
????????????clickCallBack();??
????????}??
????}??
??
}??
在活動中使用:
[html]?view plaincopyprint?
MyAlerDialog?mydialog?=?new?MyAlerDialog(this)?{??
????????????//?重寫callback方法??
????????????@Override??
????????????public?void?clickCallBack()?{??
????????????????//?TODO?Auto-generated?method?stub??
????????????????btn.setText("call");??
????????????}??
????????};??
????????mydialog.show();??
自己寫的功能就封裝了兩個,有需要的童鞋可以很容易的擴(kuò)展。這種方法,顯然相對于上一種要有優(yōu)勢得多啦。
總結(jié)
以上是生活随笔為你收集整理的Android自定义AlertDialog的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DP(01背包) UESTC 1218
- 下一篇: 前端开发学习笔记(一)深入浅出Javas