日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android自定义AlertDialog

發布時間:2023/12/20 Android 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();??

  • ????????????????//?去掉邊框的黑色,因為設置的與四周的間距為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);??


  • 這樣 ,重新給它填充自定義的布局視圖,但缺乏可擴展性,而且每次使用還得重新定義。

    ?

    ?

    重寫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();??

  • 自己寫的功能就封裝了兩個,有需要的童鞋可以很容易的擴展。這種方法,顯然相對于上一種要有優勢得多啦。

    總結

    以上是生活随笔為你收集整理的Android自定义AlertDialog的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。