Mel*_*Mel 8 android preferences
我使用下面的代码创建自定义首选项.xml布局文件有一个Button,EditText和TextView.此自定义布局显示在Alert"确定"和"取消"按钮内.这一切都运作良好.
我想在"确定"和"取消"按钮旁边添加第三个按钮(中性按钮).我已经对该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;
}
}
Run Code Online (Sandbox Code Playgroud)
我看到你的问题有点老了,也许你已经有了问题的答案,但这里有一个扩展DialogPreference的类的解决方案.
首先,您必须覆盖MelsMessage类中的onPrepareDialogBuilder方法:
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
{
super.onPrepareDialogBuilder(builder);
builder.setNeutralButton("hello", this);
}
Run Code Online (Sandbox Code Playgroud)
this在setNeutralButton方法中是DialogPreference类实现的DialogInterface.OnClickListener接口.
您要做的最后一件事是覆盖MelsMessage类中的onClick方法:
@Override
public void onClick(DialogInterface dialog, int which)
{
super.onClick(dialog, which);
switch (which)
{
case DialogInterface.BUTTON_POSITIVE:
// do things for the right button
break;
case DialogInterface.BUTTON_NEGATIVE:
// do things for the left button
break;
default:
// do things for the center button
break;
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想处理另一个类中的点击,你所要做的就是DialogInterface.OnClickListener在这个类中实现.
希望这会帮助你.干杯.
当您覆盖onCreateDialogView并且没有设置时,“确定”setPositiveButton和setNegativeButton“取消”按钮应该消失,不是吗?因为在该方法中,您将覆盖默认布局并设置自定义布局。
如果是这种情况,那么您应该创建自己的自定义底部布局,其中包含 3 个按钮,并将其添加到膨胀的布局中。尝试搜索并实现“底部 ButtonBar”,它具有所有必要的实现,因为我在文档中没有看到任何方法或方式来实现像常规对话框一样的中性按钮。
| 归档时间: |
|
| 查看次数: |
1900 次 |
| 最近记录: |