我目前有一个自定义的Dialog类,它扩展了DialogPreference(当然这是PreferenceScreen的一部分)。在DialogPreference中隐藏默认按钮
此对话框有自定义按钮,可处理保存和取消。因此,我想摆脱标准的“正面”和“负面”按钮。
尝试使用AlertDialog getButton方法,但没有成功。
我目前有一个自定义的Dialog类,它扩展了DialogPreference(当然这是PreferenceScreen的一部分)。在DialogPreference中隐藏默认按钮
此对话框有自定义按钮,可处理保存和取消。因此,我想摆脱标准的“正面”和“负面”按钮。
尝试使用AlertDialog getButton方法,但没有成功。
使用以下代替DialogPreference:
<Preference
android:title="This acts as a button"
android:key="button"
android:summary="This can act like a button to create it's own dialog"/>
然后在java:
Preference button = (Preference)findPreference("button");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
showDialog(MY_DIALOG); // let's say MY_DIALOG is 'final int MY_DIALOG = 1;' in the class body
return false;
}
});
然后添加到您的类主体:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SHOW_APP_STRING:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mylayout = inflater.inflate(R.layout.mylayout, null);
final AlertDialog myDialog = new AlertDialog.Builder(this)
.setView(mylayout)
.show();
//The buttons are below the dialog so you can close the dialog within your button listeners
Button save = (Button)myLayout.findViewById(R.id.save);
Button cancel = (Button)myLayout.findViewById(R.id.cancel);
//set onClickListeners for both of your buttons
return myDialog;
}
}
我不知道这是否是最好的方法,但这是我如何做的,它的工作原理。
如果你想创建一个自定义DialogPreference
你必须创建自己的类和扩展DialogPreference
。要隐藏按钮使用setPositiveButtonText(null);
和setNegativeButtonText(null);
在构造函数
package ...;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
public class MyDialogPreference extends DialogPreference {
public MyDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setPositiveButtonText(null);
setNegativeButtonText(null);
}
}
您可以使用'null'而不是空字符串。 – 2012-05-05 12:31:10
也可以将它们放入onPrepareDialogBuilder(AlertDialog.Builder构建器)而不是构造函数。 – Prizoff
看起来不错,但不起作用T_T –
非常感谢推动我进入正确的方向。有了你的帮助,我可以达到我想要的。 – zng
该解决方案不必要的复杂。只需将两个按钮的文本设置为空,它们就会消失。 setPositiveButtonText(NULL); setNegativeButtonText(null); 在构造函数中。 –