How can I create positive and negative buttons at custom dialogs(如何在自定义对话框中创建正面和负面按钮)
问题描述
我想创建一个自定义对话框.所以我创建了一个模板dialog_change"并打开了对话框.
I want to create a custom dialog. So i create a template 'dialog_change' and I open the dialog.
Dialog myDialog = new Dialog(Overview.this);
myDialog.setContentView(R.layout.dialog_change);
myDialog.setTitle("My Custom Dialog Title");
myDialog.show();
现在我想在底部添加两个按钮(一个正按钮和一个负按钮).我该怎么做?
Now i want to add two button (one positive and one negative button), at the bottom. How can i do that?
推荐答案
我只是制作你自己的自定义类来模拟一个 AlertDialog,这样你就可以使用你自己的布局而不附加任何字符串.(如果您想要一个完全样式化的 AlertDialog,则存在一些无法完全摆脱框架的奇怪问题).类似这样的东西,但您可以随意扩展它:
I'd just make your own custom class to simulate an AlertDialog, this way you can use your own layout with no strings attached. (There are some weird issues where you can't fully get rid of the frame if you want a fully styled AlertDialog). Something like this, but you can expand this as fully as you want:
public class CustomDialog extends Dialog {
private Button positive, negative;
public CustomDialog(Context context) {
super(context);
initialize(context);
}
protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
initialize(context);
}
public CustomDialog(Context context, int theme) {
super(context, theme);
initialize(context);
}
private void initialize(Context c) {
//Inflate your layout, get a handle for the buttons
positive = (Button)layout.findViewById(R.id.positive):
negative = (Button)layout.findViewById(R.id.negative):
positive.setVisibility(View.GONE);
negative.setVisibility(View.GONE);
}
public void setPositiveButton(String buttonText, View.OnClickListener listener) {
positive.setText(buttonText);
positive.setOnClickListener(listener);
positive.setVisibility(View.VISIBLE);
}
public void setNegativeButton(String buttonText, View.OnClickListener listener) {
negative.setText(buttonText);
negative.setOnClickListener(listener);
negative.setVisibility(View.VISIBLE);
}
}
这篇关于如何在自定义对话框中创建正面和负面按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在自定义对话框中创建正面和负面按钮
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01