android - 在同一活动中显示警报对话框
问题描述
我有AlertDialog
,但我想在同一个活动中展示。我应该在下面的代码中添加什么?
new AlertDialog.Builder(ContactAlertDialogActivity.this)
.setTitle("Permission Required")
.setMessage(("To set a contact ringtone,Old Telephone Ringtones needs access to your contacts.We never read " +
(",store or share your contact information in any way." +
"On the next screen tap Allow")))
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
})
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton("NOT NOW", null)
.setIcon(R.drawable.phone)
.show();
解决方案
尝试在活动中的任何位置为对话框创建一个方法,然后在要显示警报对话框的每个位置调用它。
private void DisplayAlertDialogBox(string _title, string _body)
{
AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic__alert)
//set title
.setTitle(_title)
//set message
.setMessage(_body)
//set positive button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
finish();
}
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
}
}).show();
}
推荐阅读
- webpack - 无法使用 webpack 导入 .gltf 文件
- java - 如何在我的 UI 中使用 vaadin 显示图片
- wordpress - .htaccess 但仍然可以访问?
- java - org.hibernate.exception.SQLGrammarException 不正确的 postgresql sql 查询
- node.js - ImageMagick 仅在使用流时转换 pdf 的第一页
- maven - 在没有 pom.xml 的情况下下载本地或全局 repo 的 maven 依赖项
- ios - iPhone和iPad设备之间的UIWindow方向不一致?
- sql - 如何修复 SQL 中的无效连接标识符
- python-3.x - 如何在kivy中的按钮按下事件上打开新窗口?
- typescript - 无法使用 TypeScript 在 Vue-cli 2 上挂载组件