AlertDialog使用和自定义

来源:互联网 发布:尼康 调焦软件 d7100 编辑:程序博客网 时间:2024/05/21 19:32

一、使用系统样式

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();        }});//显示DialogpaperDialog.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 -->
0 0
原创粉丝点击