
一、AlertDialog使用
原生的AlertDialog使用非常简单,这里直接简单贴上代码演示一遍:
Button button = findViewById(R.id.button_dialog_hello);
button.setOnClickListener(view -> {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Reminder");
alertDialog.setIcon(R.drawable.ic_launcher_background);
alertDialog.setMessage("Are you sure to delete?");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("sure", (dialogInterface, i) -> {
//Execute when OK is pressed
});
alertDialog.setNegativeButton("cancel", (dialogInterface, i) -> {
//Execute when cancel is pressed
});
alertDialog.show();
});
二、自定义Dialog的第一种实现方式
Dialog作为项目中非常常用的一种组件,可以封装成一个工具类,在该类中可以将每一d窗写成一个static修饰的方法,方法中传入需要的参数,比如Activity对象,再通过接口回调的方式实现d窗中的按钮点击事件监听。
首先我们需要写一个自己想要的d窗界面,可以根据自己的需求写,我这里简单的写一下一个输入密码的d窗:
XML中的shape代码比较简单,这里就不贴上来了,下图是d窗的界面(界面问题自行调整):
d窗方法:
private static AlertDialog dialog_password;
public static void showPasswordDialog(Activity activity, String title, String
positiveText, String negativeText
, DialogButtonOnClickListener dialogButtonOnClickListener) {
@SuppressLint("InflateParams")
View view = LayoutInflater.from(activity).inflate(R.layout.dialog_password,
null);
Button button_confirm = view.findViewById(R.id.button_dialog_confirm);
Button button_cancel = view.findViewById(R.id.button_dialog_cancel);
TextView textView_title = view.findViewById(R.id.textView_dialog_title);
EditText textView_message = view.findViewById(R.id.textView_dialog_message);
textView_title.setText(title);
button_confirm.setText(positiveText);
button_cancel.setText(negativeText);
button_confirm.setOnClickListener(view12 -> {
dialogButtonOnClickListener.onPositive(textView_message.getText().toString().trim());
dialog_password.dismiss();
});
button_cancel.setOnClickListener(view1 -> {
dialogButtonOnClickListener.onNegative();
dialog_password.dismiss();
});
AlertDialog.Builder builder_password = new AlertDialog.Builder(activity,
R.style.dialog);
builder_password.setView(view);
builder_password.setCancelable(true);
dialog_password = builder_password.create();
dialog_password.getWindow().setDimAmount(0.5f);
dialog_password.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog_password.setCanceledOnTouchOutside(true);
dialog_password.show();
}
接口:
public interface DialogButtonOnClickListener {
void onPositive(String password);
void onNegative();
}
最后是在Activity中调用d窗:
Button button = findViewById(R.id.button_dialog_hello);
button.setOnClickListener(view -> DialogUtil.showPasswordDialog(MainActivity.this
, "Please input a password", "Confirm"
, "Cancel", new DialogUtil.DialogButtonOnClickListener() {
@Override
public void onPositive(String password) {
if (password.equals("12345678")) {
Log.i("MainActivity", "The password is correct");
} else {
Log.e("MainActivity", "Password error");
}
}
@Override
public void onNegative() {
//Execute when cancel is pressed
}
}));
style代码补上:
三、第二种自定义d窗的实现方法
等下一篇文章(现在不想写了)-_-' -_-' -_-'
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)