0

我使用自定義ListPreference在用戶單擊它時顯示我的自定義對話框。它看起來像:在ListPreference上不顯示標準對話框

public class DeadLinePicker extends ListPreference{ 
    static final int DATE_DIALOG_ID = 1; 
    static final int TIME_DIALOG_ID = 0; 
    Dialog dialog; 
    DeadLinePicker instance; 
    Context context ; 
    private int mDay; 
    private int mHour; 
    private int mMinute; 
    private int mYear; 
    private int mMonth; 

    public DeadLinePicker(Context context) { 
      super(context); 
      this.context = context; 

     } 
    public DeadLinePicker(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
     final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 
     mHour = c.get(Calendar.HOUR); 
     mMinute = c.get(Calendar.MINUTE); 

    } 

    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { 
     public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) { 
      mHour = hourOfDay; 
      mMinute = minuteOfHour; 
      updateDisplay(); 

     } 
     }; 

     DatePickerDialog.OnDateSetListener mDateSetListener = 
       new DatePickerDialog.OnDateSetListener() { 
       public void onDateSet(DatePicker view, int year, int monthOfYear, 
           int dayOfMonth) { 
         mYear = year; 
         mMonth = monthOfYear; 
         mDay = dayOfMonth; 
         updateDisplay(); 
       } 

     }; 
     private CustomListPreferenceAdapter customListPreferenceAdapter; 
     protected Dialog onCreateDialog(int id) { 
       switch (id) { 
       case DATE_DIALOG_ID: 
         return new DatePickerDialog(context, 
           mDateSetListener, 
           mYear, mMonth, mDay); 

       case TIME_DIALOG_ID: 
        return new TimePickerDialog(context, mTimeSetListener, mHour, mMinute, 
         false); 

       } 
       return null; 
     } 
     protected void onPrepareDialog(int id, Dialog dialog) { 
       switch (id) { 
       case DATE_DIALOG_ID: 
         ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 

         break;} 
       } 
     public void updateDisplay() { 
      Activity activity = (Activity)context; 
      instance = new DeadLinePicker(context); 
      String minutes = null; 
      if (mMinute>9) 
       minutes = Integer.toString(mMinute); 
      else 
       minutes = "0"+Integer.toString(mMinute); 
      instance.setSummary(
        new StringBuilder() 
        // Month is 0 based so add 1 
        .append(mYear).append("-") 
        .append(mMonth + 1).append("-") 
        .append(mDay) 
        .append(" ").append(mHour).append(":").append(minutes) 
        ); 
      activity.showDialog(DATE_DIALOG_ID); 

    } 
    @Override 
     protected void onPrepareDialogBuilder(Builder builder) { 

      super.onPrepareDialogBuilder(builder); 
      builder.setNegativeButton(null, null); 
      builder.setTitle(null); 
      builder.setView(null); 
      builder.setAdapter(null, null); 
      Dialog alert = builder.create(); 
      alert.dismiss(); 
     } 
} 

我的自定義對話框顯示成功時,我打電話updateDisplay()onPreferenceClick。但是之後也會顯示標準對話框。我想讓它不顯示。我在onPrepareDialogBuilder設置了空值,但它仍然顯示。我怎樣才能不顯示?

+0

你有沒有想過如何不顯示默認對話框? – 2013-04-17 23:50:39

+0

是的,我已經添加了答案。 – 2013-04-18 08:33:47

+0

謝謝,這有幫助。 – 2013-04-18 20:03:27

回答

1

我解決了它這樣的:用自定義佈局添加footerView,這看起來像ListPreference:

public class CustomEditPreference extends RelativeLayout{ 
     private CustomTextView title; 
    private CustomTextView summary; 
    Context ctx; 
    private ImageView icon; 
     private int alpha = 255; 
    public CustomEditPreference(Context context) { 
      super(context); 

      init(context, null); 
      setWillNotDraw(false); 
     } 
    public CustomEditPreference(Context context, AttributeSet attrs) { 
      super(context, attrs); 

      init(context, attrs); 
      setWillNotDraw(false); 

     } 
    private void init(Context context, AttributeSet attrs) 
    { 

      LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.icon_pref, this); 

      title = (CustomTextView) this.findViewById(android.R.id.title); 

      summary = (CustomTextView) this.findViewById(android.R.id.summary); 
      icon = (ImageView) this.findViewById(R.id.icon); 
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomEditPreference); 
       int i= a.getInt(R.styleable.CustomEditPreference_iconDisable,0); 
       String v= a.getString(R.styleable.CustomEditPreference_android_title); 
       int alpha= a.getInt(R.styleable.CustomEditPreference_alpha, 0); 
       if (i!=0) 
       { 
        icon.setVisibility(View.GONE); 
       } 
       if (v!=null) 
        title.setText(v); 
       if (alpha !=0) 
        this.setAlpha(alpha); 
       a.recycle(); 
    } 

     public void setTitle(String text) 
     { 
      title.setText(text); 
     } 
     public void setSummary(String text) 
     { 
      summary.setText(text); 
     } 
     public String getSummary() 
     { 
      return this.summary.getText().toString(); 
     } 
     public void setIcon(int rez) 
     { 
      Resources res = getResources(); 
      Drawable icon2 = res.getDrawable(rez); 
      icon.setImageDrawable(icon2); 
     } 
     public void iconDisable() 
     { 
      icon.setVisibility(View.GONE); 
     } 
     public int getAlpha(){ 
      return alpha; 
     } 

     public void setAlpha(int opacity){ 
      if (this.alpha != opacity) { 
       alpha = opacity; 
       AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F); 
       alpha.setDuration(0); // Make animation instant 
       alpha.setFillAfter(true); // Tell it to persist after the animation ends 
       // And then on your layout 
       this.startAnimation(alpha); 
       invalidate(); 
      } 

     } 

} 

佈局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:foo="http://schemas.android.com/apk/res/com.assignmentexpert" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:paddingRight="?android:attr/scrollbarSize"> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dip" 
     android:layout_marginRight="6dip" 
     android:layout_marginTop="6dip" 
     android:layout_marginBottom="6dip" 
     android:layout_weight="1"> 
     <com.customitems.CustomTextView android:id="@+android:id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      foo:customFont="Roboto-Medium.ttf" 
      android:layout_marginLeft="25dp" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" /> 
     <com.customitems.CustomTextView 
       android:id="@+android:id/summary" 
       foo:customFont="Roboto-Medium.ttf" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="25dp" 
      android:textColor="#42aeff" 
      android:layout_below="@android:id/title" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:maxLines="2" /> 
    </RelativeLayout> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:src="@drawable/list_pointer" 
     android:layout_alignParentRight="true" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_centerVertical="true"/> 
    </RelativeLayout> 

和對話創造相同的機制,並顯示爲我張貼在上面。