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
- AlertDialog使用和自定义
- alertDialog的自定义style和使用(Android)
- alertDialog的自定义style和使用(Android)
- 自定义AlertDialog的创建和使用
- (自定义)AlertDialog使用
- 自定义ListView和AlertDialog
- 自定义PopupWindow和AlertDialog
- Android开发之AlertDialog和自定义AlertDialog
- Android中自定义AlertDialog使用
- 使用AlertDialog.Builder自定义对话框
- Android中自定义AlertDialog使用
- 使用自定义视图的AlertDialog
- AlertDialog使用自定义的布局
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- Android使用自定义AlertDialog(退出提示框)
- 2015湖南省acm省赛赛题解(部分)
- Ubuntu虚拟机下一些常见问题解决[待续]
- RasCustomDial
- codeforces 700D 哈夫曼树 莫队
- IQKeyboardManager的用法
- AlertDialog使用和自定义
- 最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器
- 监督学习和无监督学习
- Javase常见面试题目的学习笔记、知识点整理(边学边补充)
- 剑指Offer:链表中环的入口节点
- 台式机win8.1下装ubuntu16.04双系统,并解决启动项Windows系统消失的问题。
- 如何使用Python写一个爬虫软件
- csu 1020 真三国无双
- jquary实现仿百度经验的图文帮助中心轮播