这个类似于 Sweet Alert Dialog ,同样是弹出提示,这个比Sweet Alert Dialog功能要多点
ColorDialog github地址
展示:
过程:
第一步:project的build.gradle中添加maven
maven { url "https://jitpack.io" }
第二步:添加依赖并Sync Now
implementation 'com.github.andyxialm:ColorDialog:1.0.0'
开始写:
xml创建十个按钮,因为他的方法里面大概就10个主要的方法,略(可以在下面的连接中下载demo查看)
MainActivity.java
package com.example.colordialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import cn.refactor.lib.colordialog.ColorDialog;
import cn.refactor.lib.colordialog.PromptDialog;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button_1);
Button button2 = (Button) findViewById(R.id.button_2);
Button button3 = (Button) findViewById(R.id.button_3);
Button button4 = (Button) findViewById(R.id.button_4);
Button button5 = (Button) findViewById(R.id.button_5);
Button button6 = (Button) findViewById(R.id.button_6);
Button button7 = (Button) findViewById(R.id.button_7);
Button button8 = (Button) findViewById(R.id.button_8);
Button button9 = (Button) findViewById(R.id.button_9);
Button button10 = (Button) findViewById(R.id.button_10);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1: {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_SUCCESS)
.setAnimationEnable(true)
.setTitleText("SUCCESS")
.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容")
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_2: {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_DEFAULT)
.setAnimationEnable(true)
.setTitleText("DEFAULT")
.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容")
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_3: {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_HELP)
.setAnimationEnable(true)
.setTitleText("HELP")
.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容")
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_4: {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_INFO)
.setAnimationEnable(true)
.setTitleText("INFO")
.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容")
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_5: {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_WARNING)
.setAnimationEnable(true)
.setTitleText("Warning")
.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容")
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_6: {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
.setAnimationEnable(true)
.setTitleText("WRong")
.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容")
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_7: {
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle("标题");
dialog.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
dialog.setColor("#2b4490");
dialog.setContentImage(getResources().getDrawable(R.mipmap.touxiang));
dialog.setPositiveListener("确定", new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.setNegativeListener("取消", new ColorDialog.OnNegativeListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getNegativeText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_8: {
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle("标题");
dialog.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
dialog.setPositiveListener("确定", new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.setNegativeListener("取消", new ColorDialog.OnNegativeListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getNegativeText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_9: {
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle("标题");
dialog.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
dialog.setContentImage(getResources().getDrawable(R.mipmap.touxiang));
dialog.setPositiveListener("确定", new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();
}
break;
case R.id.button_10: {
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle("标题");
dialog.setContentText("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
dialog.setPositiveListener("确定", new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();
}
}
}
}
因为按钮多,所以采用这种方式
方法介绍
PromptDialog 6种弹出方式,分别为
DIALOG_TYPE_SUCCESS DIALOG_TYPE_DEFAULT DIALOG_TYPE_HELP DIALOG_TYPE_INFO DIALOG_TYPE_WARNING DIALOG_TYPE_WRONG
、
如图的6种样式
代码为
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_WRONG)//设置样式
.setAnimationEnable(true)//是否有动画
.setTitleText("CSDN")//标题
.setContentText("CSDN莫言情难忘")//内容
.setPositiveListener("OK", new PromptDialog.OnPositiveListener() {//设置监听
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();//展示
ColorDialog 4种方式
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle("标题");//标题
dialog.setContentText("CSDN莫言情难忘");//内容
dialog.setColor("#2b4490");//颜色(可以没有)
dialog.setContentImage(getResources().getDrawable(R.mipmap.touxiang));//图片(可以没有)
dialog.setPositiveListener("确定", new ColorDialog.OnPositiveListener() {//设置确定监听
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.setNegativeListener("取消", new ColorDialog.OnNegativeListener() {//取消监听
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getNegativeText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();//展示
demo下载
链接: https://pan.baidu.com/s/1dqCvt5pbCNqu4Hpcl_7S9w
提取码: 6666