0

我创建了一个类,我想在其中为日期选择器设置两个不同的微调器。我想要这个是因为我想设置开始事件日期和结束事件日期,那么如何在一个类中设置两个带有日期选择器的不同微调器???提前致谢。

4

1 回答 1

0

这是我在一些应用程序中使用的三重数字选择器。应该很容易将其修改为仅使用两个数字。

包 com.t3hh4xx0r.nfcsecure.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

import com.t3hh4xx0r.nfcsecure.R;

public class TripleNumberPickerPreference extends DialogPreference {
    private static final String defaultPersistedString = "0:0:0";
    private NumberPicker hours = null;
    private NumberPicker mins = null;
    private NumberPicker secs = null;

    View rootView;

    public TripleNumberPickerPreference(Context ctxt) {
        this(ctxt, null);
    }

    public TripleNumberPickerPreference(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, 0);
    }

    public TripleNumberPickerPreference(Context ctxt, AttributeSet attrs, int defStyle) {
        super(ctxt, attrs, defStyle);

        setPositiveButtonText("Set");
        setNegativeButtonText("Cancel");
    }

    @Override
    protected View onCreateDialogView() {
        String[] nums = new String[61];

        for(int i=0; i<nums.length; i++) {
           nums[i] = Integer.toString(i);
        }

        LayoutInflater lI = LayoutInflater.from(getContext());
        rootView = lI.inflate(R.layout.triple_number_picker, null);
        hours = (NumberPicker) rootView.findViewById(R.id.hours);
        mins = (NumberPicker) rootView.findViewById(R.id.minutes);
        secs = (NumberPicker) rootView.findViewById(R.id.seconds);

        hours.setMaxValue(24);
        hours.setMinValue(0);
        hours.setDisplayedValues(nums);     

        mins.setMaxValue(60);
        mins.setMinValue(0); 
        mins.setDisplayedValues(nums);

        secs.setMaxValue(60);
        secs.setMinValue(0); 
        secs.setDisplayedValues(nums);

        deconstructPersistedData();

        return (rootView);
    }

    private void deconstructPersistedData() {
        hours.setValue(Integer.parseInt(getPersistedString(defaultPersistedString).split(":")[0]));
        mins.setValue(Integer.parseInt(getPersistedString(defaultPersistedString).split(":")[1]));
        secs.setValue(Integer.parseInt(getPersistedString(defaultPersistedString).split(":")[2]));
    }

    private String buildPersistedData() {
        StringBuilder sB = new StringBuilder();
        sB.append(hours.getValue());
        sB.append(":");
        sB.append(mins.getValue());
        sB.append(":");
        sB.append(secs.getValue());
        sB.append(":");

        long timeTillNextSecureLock = buildTimeTillSecureLock();
        Log.d("TIME TILL NEXT LOCK SET", ""+timeTillNextSecureLock);
        sB.append(timeTillNextSecureLock);
        return sB.toString();
    }

    private long buildTimeTillSecureLock() {
        double hoursSimple = hours.getValue();
        double minsSimple = mins.getValue();
        double secsSimple = secs.getValue();

        double secsToMillis = 1000;
        double minsToMillis = 60000;
        double hoursToMillis = 3600000;

        double secsAsMills = secsSimple * secsToMillis;
        double minsAsMills = minsSimple * minsToMillis;
        double hoursAsMills = hoursSimple * hoursToMillis;

        return (long) (secsAsMills + minsAsMills + hoursAsMills);
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        String persisted = buildPersistedData();

        if (positiveResult) {
            if (callChangeListener(persisted)) {
                persistString(persisted);
                notifyChanged();
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return defaultPersistedString;
    }

      public static long subAndCheck(long a, long b) {
          long ret;
          String msg = "overflow: subtract";
          if (b == Long.MIN_VALUE) {
              if (a < 0) {
                  ret = a - b;
              } else {
                  throw new ArithmeticException(msg);
              }
          } else {
              ret = addAndCheck(a, -b, msg);
          }
          return ret;
      }

      private static long addAndCheck(long a, long b, String msg) {
          long ret;
          if (a > b) {
              ret = addAndCheck(b, a, msg);
          } else {            
              if (a < 0) {
                  if (b < 0) {
                      if (Long.MIN_VALUE - b <= a) {
                          ret = a + b;
                      } else {
                          throw new ArithmeticException(msg);
                      }
                  } else {
                      ret = a + b;
                  }
              } else {
                  if (a <= Long.MAX_VALUE - b) {
                      ret = a + b;
                  } else {
                      throw new ArithmeticException(msg);
                  }
              }
          }
          return ret;
      }
} 

和布局 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="@string/hours"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView1"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/minutes"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="@string/seconds"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <NumberPicker
            android:id="@+id/hours"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="3dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <NumberPicker
            android:id="@+id/minutes"
            android:layout_width="0dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="3dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <NumberPicker
            android:id="@+id/seconds"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
于 2013-03-20T16:19:15.797 回答