Android -- 自定义Dialog
Android -- 自定义 Dialog
2023-09-11 14:18:14 时间
⒈
设计视图
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000000" android:text="提示" android:textStyle="bold" android:layout_marginTop="20dp"/> <TextView android:id="@+id/tv_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000000" android:text="删除" android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/> <view android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#817F7F"/> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="取消" android:textSize="20sp" android:textColor="#A62518BB" android:gravity="center"/> <view android:layout_width="0.5dp" android:layout_height="match_parent" android:background="#A3A3A3"/> <TextView android:id="@+id/tv_confirm" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="确定" android:textSize="20sp" android:textColor="#A62518BB" android:gravity="center"/> </LinearLayout> </LinearLayout>
设计自定义的类
public class CustomDialog extends Dialog implements View.OnClickListener { private TextView mTvTitle,mTvMessage,mTvCancel,mTvConfirm; private String title,message,cancel,confirm; private IOnCancelListener cancelListener; private IOnConfirmListener confirmListener; public void setTitle(String title) { this.title = title; } public void setMessage(String message) { this.message = message; } public void setCancel(String cancel,IOnCancelListener cancelListener) { this.cancel = cancel; this.cancelListener = cancelListener; } public void setConfirm(String confirm,IOnConfirmListener confirmListener) { this.confirm = confirm; this.confirmListener = confirmListener; } public CustomDialog(@NonNull Context context) { super(context); } public CustomDialog(@NonNull Context context, int themeResId) { super(context, themeResId); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_custom_dialog); //设置宽度 WindowManager m = getWindow().getWindowManager(); Display d = m.getDefaultDisplay(); WindowManager.LayoutParams p = getWindow().getAttributes(); Point size = new Point(); d.getSize(size); p.width = (int)(size.x * 0.8); //设置dialog的宽度为当前手机屏幕的宽度 * 0.8 getWindow().setAttributes(p); mTvTitle = findViewById(R.id.tv_title); mTvMessage = findViewById(R.id.tv_message); mTvCancel = findViewById(R.id.tv_cancel); mTvConfirm = findViewById(R.id.tv_confirm); if(!TextUtils.isEmpty(title)){ mTvTitle.setText(title); } if(!TextUtils.isEmpty(message)){ mTvMessage.setText(message); } if(!TextUtils.isEmpty(cancel)){ mTvCancel.setText(cancel); } if(!TextUtils.isEmpty(confirm)){ mTvConfirm.setText(confirm); } mTvCancel.setOnClickListener(this); mTvConfirm.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_cancel: if(cancelListener != null){ cancelListener.onCancel(this); } dismiss(); break; case R.id.tv_confirm: if(confirmListener != null){ confirmListener.onConfirm(this); } dismiss(); break; } } public interface IOnCancelListener{ void onCancel(CustomDialog dialog); } public interface IOnConfirmListener{ void onConfirm(CustomDialog dialog); } }
相关文章
- Android平台Camera实时滤镜实现方法探讨(十)--代码地址以及简单介绍(20160118更新)
- Android新手入门2016(7)--布局
- Android内存泄露之开篇
- Android之 ImageView android:src和tools:src的区别是什么?
- Android--获取当前屏幕的旋转角度
- 为什么Android使用弱加密算法?
- Android入门之简单拨号器
- android应用开发之Window,View和WindowManager .
- Android开发工程师文集-1 小时学会Widget小组件开发
- android:id设置的三种方式区别在哪?
- Android Tab切换
- Android studio的错误:radle sync failed: Cause: failed to find target android-21 :
- 《Android游戏开发详解》一2.13 调用对象的行为
- Android中通过typeface设置字体
- android:Android中用文件初始化sqlite数据库
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- Android API 中文(76)——AdapterView.OnItemLongClickListener
- android开发:Android 中自定义View的应用
- Android -- Android广播
- Android -- Handler消息处理
- Android -- Android的事件处理
- Android Application的目录结构
- 解决Android SDK Manager下载问题和android studio每次启动都要在fetching Android sdk compoment information
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- 技术总结--android篇(一)--MVC模式
- Android打印机--小票打印格式及模板设置
- ANDROID 中设计模式的採用--结构型模式
- [Android Studio]Android 数据存储--SQLite数据库存储
- 【Android Studio】DDMS的模拟器控制(Emulator Control)不可用
- Android 集成Chrome 浏览器内核 Crosswalk
- Android Studio Error:Could not find com.android.tools.build:gradle