AlertDialog使用和自定义
2016-08-19 20:53
85 查看
一、使用系统样式
AlertDialog.Builder builder = new AlertDialog.Builder(PaperActivity.this); builder.setMessage(message); builder.setTitle("提示"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show();
二、使用自定义样式
自定义style
<style name="AlertDialogCustom" parent="android:Theme.Material.Dialog.Alert"> <item name="android:textColor">#ffffff</item> <item name="android:typeface">monospace</item> <item name="android:textSize">10sp</item> </style>
使用自定义
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(PaperActivity.this, R.style.AlertDialogCustom));
三、自定义Dialog
参考链接1.Dialog布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="40px" android:minWidth="300px" android:orientation="vertical"> <TextView android:id="@+id/paper_dialog_titleText" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/colorAccent" android:text="提示" android:textSize="@dimen/alertDialogTitle"/> <TextView android:id="@+id/paper_dialog_contentText" android:layout_width="match_parent" android:minHeight="100px" android:layout_height="wrap_content" android:text="nihao" android:textSize="@dimen/alertDialogText"/> <Button android:id="@+id/paper_dialog_checkButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定" android:gravity="center" android:background="@drawable/paper_dialog_checkbutton_selector" android:textSize="@dimen/alertDialogCheck" android:textColor="@color/colorAccent"/> </LinearLayout>
2. 自定义Dialog的java
public class PaperDialog extends Dialog { private Context mContext; private TextView mTitle; private TextView mContent; private Button mCheckButton; public PaperDialog(Context context) { super(context); mContext = context; } public PaperDialog(Context context, int themeResId) { super(context, themeResId); mContext = context; } protected PaperDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } public void createDialog(){ //获得inflater LayoutInflater inflater = LayoutInflater.from(mContext); //从xml转换为View View layoutView = inflater.inflate(R.layout.question_paper_dialog, null); //PaperDialog将layoutView作为其View addContentView(layoutView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));//View的大小 //标题 mTitle = (TextView) layoutView.findViewById(R.id.paper_dialog_titleText); //内容 mContent = (TextView) layoutView.findViewById(R.id.paper_dialog_contentText); //确认按钮 mCheckButton = (Button) layoutView.findViewById(R.id.paper_dialog_checkButton); } public void setTitle(String title){ this.mTitle.setText(title); } public void setTitleSize(float size){ this.mTitle.setTextSize(size); } public void setMessage(CharSequence message) { mContent.setText(message); } public void setMessageSize(float size){ this.mContent.setTextSize(size); } public void setCheck(CharSequence checkContent) { mCheckButton.setText(checkContent); } public void setCheckSize(float size){ this.mCheckButton.setTextSize(size); } public void setCheckOnClickListener(String check, View.OnClickListener onClickListener){ this.mCheckButton.setText(check); mCheckButton.setOnClickListener(onClickListener); } }
3.对自定义Dialog进行使用
PaperDialog paperDialog = new PaperDialog(PaperActivity.this, R.style.AlertDialogCustom); paperDialog.createDialog(); //显示“提示” paperDialog.setTitle("提示"); String msg = "没有完成,无法提交试卷!"; //设置信息 paperDialog.setMessage(msg); //设置确认监听器 paperDialog.setCheckOnClickListener("确认", new View.OnClickListener() { @Override public void onClick(View v) { paperDialog.dismiss(); } }); //显示Dialog paperDialog.show();
4.style和dimens.xml
<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert"> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> </style>
<!-- question begin --> <dimen name="alertDialogTitle">30dp</dimen> <dimen name="alertDialogCheck">30dp</dimen> <dimen name="alertDialogText">25dp</dimen> <!-- question end -->
相关文章推荐
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- 自定义AlertDialog的创建和使用
- 2.5.3 使用alertDialog创建自定义对话框
- Android使用自定义AlertDialog(退出提示框)
- Android中的AlertDialog使用示例五(自定义对话框)
- AlertDialog中使用自定义View,view中的Button如何设置点击事件监听
- Android使用自定义AlertDialog(退出提示框)
- 使用自定义视图的AlertDialog
- 使用 AlertDialog 创建列表对话框及自定义对话框
- alertDialog的自定义style和使用(Android)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android开发(35) Android使用自定义AlertDialog(退出提示框)
- Android使用自定义alertdialog实现确认退出按钮