0

当用户单击它时,我使用自定义 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但它仍然显示。我怎样才能不显示?

4

1 回答 1

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>

以及与我在上面发布的相同的对话框创建和显示机制。

于 2013-04-18T08:33:00.200 回答