AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setMessage("You have system write settings permission now.");
alertDialog.show();
private void showNormalDialogOne() {
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
*/
final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
normalDialog.setTitle("提示");
normalDialog.setMessage("确定删除所有!");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// delehistory();//dosomething
}
});
normalDialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
normalDialog.show();
}
单选项dialog
AlertDialog.Builder builder = new AlertDialog.Builder(SynthActivity.this, android.R.style.Theme_Holo_Light_Dialog);
builder.setTitle("引擎空闲时切换");
final Map<String, String> map = new LinkedHashMap<>(4);
map.put("离线女声", OfflineResource.VOICE_FEMALE);
map.put("离线男声", OfflineResource.VOICE_MALE);
map.put("离线度逍遥", OfflineResource.VOICE_DUXY);
map.put("离线度丫丫", OfflineResource.VOICE_DUYY);
final String[] keysTemp = new String[4];
final String[] keys = map.keySet().toArray(keysTemp);
builder.setItems(keys, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
loadModel(map.get(keys[which]));
Log.i("lgq","....."+map.get(keys[which]));
}
});
builder.show();

2、多选项dialog
private String[] favor = {"美容 ", "汽车 ", "游戏 ", "社交 ", "体育 ", "阅读 ",
"影视 ", "母婴 ", "健康 ", "家居 ", "服饰 ", "其他 "};
private String hobby;
private int befoid;
//兴趣爱好设置
private void hobbySetting() {
AlertDialog.Builder dialogm = new AlertDialog.Builder(this);
dialogm.setMultiChoiceItems(favor, new boolean[]{false, false, false, false, false, false, false, false, false, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (!TextUtils.isEmpty(hobby)&&hobby.split("\\ ").length > 2&&befoid!=which) {
// ToastUtil.centralToast("最多只能选择3项", mContext);
dialog.dismiss();
hobby = "";
befoid = 99;
} else {
if (isChecked){
hobby = hobby + favor[which];
befoid = which;
Log.v("lgq","兴趣爱好。。。。。-====="+hobby);
}
else {
hobby = hobby.replace(favor[which], "");
befoid = 99;
Log.v("lgq","兴趣爱好。。。else。。==="+hobby);
}
}
}
});
dialogm.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (hobby.equals("")) tvHobby.setText("未设置");
else tvHobby.setText(hobby);
hobby = "";
}
});
dialogm.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialogm.show();
}

默认提示框
public class DialogUtils {
//自定义View对话框
public static Dialog show(Context context, View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setView(view)
.setCancelable(true);
Dialog dialog = builder.show();
dialog.getWindow().getDecorView().setBackground(null);
return dialog;
}
}
调用
//软件说明对话框
public void showDescription() {
View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_description, null);
//软件说明
Button btnDismiss = view.findViewById(R.id.btn_dismiss);
mTxvModeChoose = view.findViewById(R.id.txv_mode_choose);
final Dialog dialog = DialogUtils.show(mContext, view);
btnDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击右上角 × 关闭弹窗
dialog.dismiss();
}
});
mTxvModeChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击选择模式
mTxvModeChoose.setBackgroundResource(R.drawable.txv_stroke);
mTxvModeFill.setBackground(null);
mTxvDictionary.setBackground(null);
mTxvDescription.setText(R.string.str_descrip_choose);
}
});
}
点击事件Dialog
public class TipDialog2 extends Dialog {
@BindView(R.id.Cancel_tv)
TextView mCancelTv;
@BindView(R.id.confirm_tv)
TextView mConfirmTv;
@BindView(R.id.tv_content)
TextView mTvContent;
@BindView(R.id.tv_title)
TextView mTvTitle;
private CallBack mCallBack;
public TipDialog2(@NonNull Context context) {
super(context, R.style.CommonDialogStyle);
setContentView(R.layout.dialog_tip2);
ButterKnife.bind(this);
}
@OnClick({ R.id.Cancel_tv, R.id.confirm_tv })
public void onClick(View view) {
switch (view.getId()) {
case R.id.Cancel_tv:
dismiss();
break;
case R.id.confirm_tv:
if (mCallBack != null) {
mCallBack.confirm();
}
dismiss();
break;
}
}
public void setCallBack(CallBack callBack) {
this.mCallBack = callBack;
}
public interface CallBack {
void confirm();
}
public void setTitle(boolean isVisible,String title){
mTvTitle.setVisibility(isVisible?View.VISIBLE:View.GONE);
mTvTitle.setText(title);
}
public void setTvContent(String content) {
mTvContent.setText(content);
}
}
调用
TipDialog2 tipDialog2 = new TipDialog2(this);
tipDialog2.setCallBack(new TipDialog2.CallBack() {
@Override
public void confirm() {
finish();
}
});
tipDialog2.show();
EditText键盘不弹出
//弹出软键盘
public void showKeyboard(EditText editText) {
//其中editText为dialog中的输入框的 EditText
if(editText!=null){
//设置可获得焦点
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
//请求获得焦点
editText.requestFocus();
//调用系统输入法
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}
本文介绍了在Android应用中创建和使用多种类型的对话框的方法,包括简单的消息对话框、单选对话框、多选对话框及自定义视图对话框等,并提供了具体的代码实现示例。
8855

被折叠的 条评论
为什么被折叠?



