如何使用中性按钮创建自定义首选项?
我使用下面的代码来创建自定义首选项。 xml 布局文件有 Button
、EditText
和 TextView
。此自定义布局出现在带有“确定”和“取消”按钮的警报
内。这一切都运作良好。
我想在“确定”和“取消”按钮旁边添加第三个按钮(中性按钮)。我已经尝试过 AlertBuilder
类,但不知道如何合并我的自定义xml 布局和中性按钮
如何做到?
目前
public class MelsMessage extends DialogPreference {
Button bMessage;
EditText eMessage;
TextView tMessage;
public MelsMessage(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected View onCreateDialogView() {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View view = layoutInflater.inflate(R.layout.dialog_pref_mess, null);
//UI elements
bMessage = (Button) view.findViewById(R.id.buttonMessage);
eMessage = (EditText) view.findViewById(R.id.edittextMessage);
tMessage = (TextView) view.findViewById(R.id.textviewMessage);
return view;
}
}
I use the code below to create a custom preference. The xml layout file has a Button
, EditText
and TextView
. This custom layout appears inside an Alert
with "OK" and "Cancel" buttons. This all works well.
I would like to add a third button (a neutral button) beside the "OK and "Cancel" buttons. I've experimented with the AlertBuilder
class but can't figure out how to incorporate both my custom xml layout and a neutral button.
How can this be done?
Currently have...
public class MelsMessage extends DialogPreference {
Button bMessage;
EditText eMessage;
TextView tMessage;
public MelsMessage(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected View onCreateDialogView() {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View view = layoutInflater.inflate(R.layout.dialog_pref_mess, null);
//UI elements
bMessage = (Button) view.findViewById(R.id.buttonMessage);
eMessage = (EditText) view.findViewById(R.id.edittextMessage);
tMessage = (TextView) view.findViewById(R.id.textviewMessage);
return view;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现您的问题有点老了,也许您已经有了问题的答案,但这里有一个扩展 DialogPreference 的类的解决方案。
首先,您必须重写 MelsMessage 类中的
onPrepareDialogBuilder
方法:setNeutralButton
中的this
code> 方法是 DialogPreference 类实现的DialogInterface.OnClickListener
接口。您要做的最后一件事是覆盖 MelsMessage 类中的
onClick
方法:如果您想在另一个类中处理所有点击你所要做的就是在这个类中实现
DialogInterface.OnClickListener
。希望这会对您有所帮助。干杯。
I see your question is a bit old and maybe you already have the answer to your question but here is a solution for your class that extends DialogPreference.
First you have to Override the
onPrepareDialogBuilder
method in your MelsMessage class:this
in thesetNeutralButton
method isDialogInterface.OnClickListener
interface that the DialogPreference class implement.The last thing you have to do is to Override the
onClick
method in your MelsMessage class:If you want to handle the click in another class all you have to do is to implement
DialogInterface.OnClickListener
in this class.Hope this will help you. Cheers.
当您重写 onCreateDialogView 且未设置 setPositiveButton 和 setNegativeButton 时,“确定”和“取消”按钮应该消失,不是吗?因为在该方法中,您将覆盖默认布局并设置自定义布局。
如果是这种情况,那么您应该创建自己的自定义底部布局,其中包含 3 个按钮,并将其添加到膨胀的布局中。尝试搜索并实现“底部 ButtonBar”,它具有所有必要的实现,因为我在文档中没有看到任何方法或方式来实现像常规对话框一样的中性按钮。
When you are overriding
onCreateDialogView
and you don't set thesetPositiveButton
andsetNegativeButton
the ok and cancel button should disappear, don't they? Because in that method you are overriding the default layout and setting a custom one.If that is the case then you should create you own custom bottom layout with 3 buttons and add it in the inflated one. Try searching and implementing the "bottom ButtonBar" which has all the necessary implementation, because I don't see in the docs any method or a way to implement the neutral button like on a regular Dialog.
您可以创建带有 3 个按钮的自定义对话框或编写如下代码
you can create custom Dialog with 3 buttons or write code like