[英]Hide default buttons in DialogPreference
我目前有一個自定義Dialog類,它擴展了DialogPreference(它當然是PreferenceScreen的一部分)。
此對話框具有自定義按鈕,用於處理保存和取消。 因此,我想擺脫標准的“正”和“負”按鈕。
嘗試使用AlertDialog getButton方法,但未成功。
如果要創建自定義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);
}
}
在您的xml中,使用以下內容代替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;
}
}
我不確定這是否是最好的方法,但是這就是我的方法,並且有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.