Foreword:
A long time ago, I wrote an article to introduce the "custom dialog prompt box high imitation QQ browser version update prompt box " of custom dialog, hey, it is simply weak, if you write code in that way , it is estimated that it is difficult to maintain, and the code is not intuitive, so I encapsulated the custom dialog, and encapsulated some repeatedly used code into a method to call directly, how refreshing it is.
demo
Instructions:
Step 1
Gradle
dependencies {
compile 'com.superluo:dialog:1.0.1'
}
Maven
<dependency>
<groupId>com.superluo</groupId>
<artifactId>dialog</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
Step 2
Create the dialog layout you want to achieve in xml, just like it, haha.
Step 3
Next, you can call the encapsulated dialog tool class through concise code to achieve the effect you want.
(1) Simple call:
new CustomDialog.Builder(this)
.view(R.layout.xxx)
.build()
.show();
(2) Use the addViewOnclick(viewId, listener) method to add the click listener of the view:
new CustomDialog.Builder(this)
.view(R.layout.xxx)
.addViewOnclick(R.id.xxx,listener)
.build()
.show();
(3) Use the cancelTouchout(boolean) method to set whether to click the dialog background to cancel the dialog (default is true):
new CustomDialog.Builder(this)
.view(R.layout.xxx)
.addViewOnclick(R.id.xxx,listener)
.cancelTouchout(false)
.build()
.show();
(4) Use the setDialogAnim(R.style.xxx) method to set the dialog in and out animation:
new CustomDialog.Builder(this)
.view(R.layout.xxx)
.addViewOnclick(R.id.xxx,listener)
.cancelTouchout(false)
.setDialogAnim(R.style.xxx)
.build()
.show();
(5)使用setDialogPosition(position)方法设置dialog在屏幕的显示位置 (默认为屏幕中间):
new CustomDialog.Builder(this)
.view(R.layout.xxx)
.addViewOnclick(R.id.xxx,listener)
.cancelTouchout(false)
.setDialogAnim(R.style.xxx)
.setDialogPosition(xxx)
.build()
.show();
(6)设置dialog宽高,默认宽为屏幕的0.7,高为包裹内容。下面提供了几种设置宽高的方法(调用方法如上):
setHeightPX(int val)//用px设置dialog高度
setWidthPX(int val)//用px设置dialog宽度
setHeightDP(int val)//用dp设置dialog高度
setWidthDP(int val)//用dp设置dialog宽度
setHeightDimenRes(int dimenRes)//用dimen设置dialog高度
setWidthDimenRes(int dimenRes)//用dimen设置dialog宽度
(7)使用cancel()方法取消dialog显示:
CustomDialog dialog = new CustomDialog.Builder(this)
.view(R.layout.xxx)
.addViewOnclick(R.id.xxx,listener)
.cancelTouchout(false)
.setDialogAnim(R.style.xxx)
.setDialogPosition(xxx)
.build();
//dialog显示
dialog.show();
、、、
//取消dialog显示
dialog.cancel();
(8)最后还可以使用获取控件实例的方法,以便在代码里设置控件的文本属性;
dialog = new CustomDialog.Builder(this)
.view(R.layout.dialog_custom_view1)
.addViewOnclick(R.id.tv_cancel,listener)
.addViewOnclick(R.id.tv_update,listener)
.build();
//代码设置view属性
TextView tv_title = dialog.findView(R.id.tv_title);
TextView tips = dialog.findView(R.id.tv_tips);
TextView tv_update = dialog.findView(R.id.tv_update);
tv_title.setText("辞职信");
tips.setText("由于程序猿这行业是高危行业,所以我决定明天就向老板申请离职,毕竟世界那么大,我还想活着去看看");
tv_update.setText("立即辞职");
dialog.show();
demo下载地址:GitHub