如何通过编程方式关闭android中的Dialog,例如按一个按钮?
想象一下,我有一个带有OK按钮的Dialog,想要通过OK按钮关闭它,但我不能这样做!
我用Google搜索并发现没什么用处,几乎所有这些都用于关闭AlertDialog而不是Dialog.
正面,负面和中性按钮之间是否存在功能差异,特别是在AlertDialogs的上下文中?
我一直试图用圆角制作我的警报对话但不知何故我无法做到.我试过,但我失败了.我试着关注这个博客http://blog.stylingandroid.com/archives/271并根据它制作了我的风格.
顺便说一句,现在就加我的问题.我的一些新发现.上面链接中的代码在2.3.3(GB)上工作正常但在ICS中根本不起作用.一些改变使代码破裂.
我想避免创建9个补丁图像,因此我使用的是形状.9补丁图像是我将尝试的最后一件事.我知道android警报对话框样式正在使用9补丁图像.在抛出这个问题之前我已经看过了.
/res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Dialog">
<item name="android:alertDialogStyle">@style/dialog</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
/res/values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="android:Theme.Light" />
<style name="myImageView">
<!-- 3dp so the background border to be visible -->
<item name="android:padding">3dp</item>
<item name="android:background">@drawable/image_drawable</item>
<item name="android:scaleType">fitCenter</item>
</style>
<style name="dialog">
<item name="android:fullDark">@drawable/dialog_body</item>
<item name="android:topDark">@drawable/dialog_title</item>
<item name="android:centerDark">@drawable/dialog_body</item>
<item name="android:bottomDark">@drawable/dialog_footer</item>
<item name="android:fullBright">@drawable/dialog_body</item>
<item name="android:centerBright">@drawable/dialog_body</item>
<item name="android:topBright">@drawable/dialog_title</item>
<item name="android:bottomBright">@drawable/dialog_footer</item>
<item name="android:bottomMedium">@drawable/dialog_footer</item>
<item name="android:centerMedium">@drawable/dialog_body</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
/res/drawable/dialog_title.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="-1dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:topLeftRadius="5dp" android:topRightRadius="5dp" /> …
Run Code Online (Sandbox Code Playgroud) 我想用透明背景显示警告对话框.我的警报对话框代码是:
AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this);
LayoutInflater inflater = (LayoutInflater)SubProducts.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.cust_toast_layout,(ViewGroup)findViewById(R.id.linearLayout2));
ImageView image = (ImageView)layout.findViewById(R.id.imageView1);
image.setPadding(0, 20, 0, 0);
imgLoader.DisplayImage(image_url, loader, image);
TextView tprice=(TextView)layout.findViewById(R.id.pricetext);
tprice.setText("$ "+pricedouble);
TextView tvdprh=(TextView)layout.findViewById(R.id.textView1);
tvdprh.setText(prohd);
WebView wv=(WebView)layout.findViewById(R.id.webview);
Spanned sub=Html.fromHtml(descp);
String s = "<html><head><style type='text/css' >@font-face {font-family:'myfont';src: url('file:///android_asset/fonts/ABeeZee-Regular.ttf');}body {margin:0px;color:000000;font-family: myfont;"
+ "text-align: justify;}</style></head><body>"
+ sub
+ "</body></html>";
wv.loadDataWithBaseURL("", s, "text/html", "utf-8", null);
wv.setVerticalScrollBarEnabled(true);
wv.setBackgroundColor(Color.TRANSPARENT);
wv.setPadding(5, 25, 5, 0);
ImageView imgcartl=(ImageView)layout.findViewById(R.id.imageView2);
imgcartl.setBackgroundResource(R.drawable.cartlines);
ImageView brobutton=(ImageView)layout.findViewById(R.id.imageView3);
brobutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intentlabl …
Run Code Online (Sandbox Code Playgroud) 我无法关闭警报对话框.我正在使用布局充气器进行对话,所以我不确定在完成它之后我会怎样关闭它.代码如下:
AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();
//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);
TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);
add_item.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//Need this to close the alert dialog box
}
});
title.setText(workout_items[position]);
dialog.setView(layout);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
我无法调用完成,因为它关闭了启动警报对话框的列表视图,并且对我无法使用dialog.dismiss调用.
您认为我应该怎么做才能解决这个问题?
我无法在AlertDialog中设置Single Choice列表或Multiple Choice List.
我试着按照这些例子,但我只得到一个带有标题的对话框,确认和取消按钮,没有列表,没有消息(我设置了!).
这是代码:
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DELETE_CITY:
CharSequence[] array = {"Red", "Blue", "Yellow"};
return new AlertDialog.Builder(ShowPypData.this)
.setTitle(R.string.city_actions_delete_label)
.setMessage(R.string.city_actions_delete_select_label)
.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的警报对话框设置android Theme.Light主题,但到目前为止没有成功.在阅读了一些教程后,我使用AlertDialog.Builder收集了这些教程,我们无法直接在构造函数中设置主题(至少在API级别7中).
我找到的替代解决方案是使用ContextThemeWrapper,每个人都保证会解决我的问题.所以我编写了这样的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(context, R.style.popup_theme));
Run Code Online (Sandbox Code Playgroud)
我在values文件夹中描述了我的主题:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="back_color">#ffffffff</color>
<style name="popup_theme" parent="@android:style/Theme.Light">
<item name="android:windowBackground">@color/back_color</item>
<item name="android:colorBackground">@color/back_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我仍然得到默认的Theme.Dialog.Alert主题.谁能告诉我为什么?我哪里错了?
编辑:如果您不知道我的问题的答案,请投票.我有一个坏习惯,发布被卡住的问题:(
这是我的警报代码,按下按钮时会显示一条消息.如何制作一个OK按钮以解散?
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setCancelMessage(null);
alert.show();
}
});
}}
Run Code Online (Sandbox Code Playgroud)
我已经把alert.setCancelMessage(null)但它没有显示任何要关闭的按钮.
谢谢你的所有答案.
解:
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK", null);
alert.show();
}
});
Run Code Online (Sandbox Code Playgroud) 我将手机更新到Android 6.0,我有两个对话框问题:
1)显示标题,但消息不用于警告对话框(已解决):
new AlertDialog.Builder(context).setTitle("Title").setMessage("Message");
Run Code Online (Sandbox Code Playgroud)
2)也没有显示自定义对话框片段的标题(未解决):
getDialog().setTitle("Title");
Run Code Online (Sandbox Code Playgroud)
在棒棒糖或旧版本中没有这样的问题,只有在将手机更新为棉花糖后才出现问题.
如何解决问题?
android android-alertdialog android-dialog android-dialogfragment android-6.0-marshmallow
我有一个弹出窗口,可以在我的应用程序中下载音频指令.我想要做的是我想将"OK"的默认文本颜色更改为蓝色.我试了一下,但它不起作用.这是我的代码:
private void showDownloadPgmPopup() {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity());
builder.setTitle("Download instructional audio?");
builder.setMessage(ParamConstants.AUDIODOWNLOADPERMISSION);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
WwDatabaseHelper.storeSelectedWeekAndDay(getActivity(), mSelectedWeekDataModel);
goToMoveScreen();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AndroidDownloadFileByProgressBarActivity.StartAudioAssetDownload(getActivity());
}
}).create();
// change the text color of download instruction ok button
final android.app.AlertDialog dialog = builder.show();
dialog.setOnShowListener( new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#ff5722"));
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
但这种变化并没有反映任何人都可以告诉我我做错了什么?