如何在android中为一个类中的日期选择器创建两个微调器?
我创建了一个类,我想在其中为日期选择器设置两个不同的微调器。我之所以要这样做,是因为我想设置开始事件日期和结束事件日期,那么如何在一个类中设置两个不同的带有日期选择器的微调器???提前感谢。这是我在一些应用程序中使用的三位数选择器。应该很容易修改为只使用两个数字 包com.t3hh4xx0r.nfcsecure.widgets如何在android中为一个类中的日期选择器创建两个微调器?,android,android-spinner,android-datepicker,Android,Android Spinner,Android Datepicker,我创建了一个类,我想在其中为日期选择器设置两个不同的微调器。我之所以要这样做,是因为我想设置开始事件日期和结束事件日期,那么如何在一个类中设置两个不同的带有日期选择器的微调器???提前感谢。这是我在一些应用程序中使用的三位数选择器。应该很容易修改为只使用两个数字 包com.t3hh4xx0r.nfcsecure.widgets import android.content.Context; import android.content.res.TypedArray; import android
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;
}
}
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.preference.DialogPreference;
导入android.util.AttributeSet;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.NumberPicker;
导入com.t3hh4xx0r.nfcsecure.R;
公共类TripleNumberPickerPreference扩展了DialogPreference{
私有静态最终字符串defaultPersistedString=“0:0:0”;
私密号码呼叫小时数=空;
私有NumberPicker mins=null;
私有NumberPicker secs=null;
视图根视图;
公共三元组PerPickerReference(上下文ctxt){
这个(ctxt,null);
}
公共TripleNumberPickerPreference(上下文ctxt,属性集attrs){
这(ctxt,attrs,0);
}
公共TripleNumberPickerPreference(上下文ctxt、属性集ATTR、int defStyle){
超级(ctxt、ATTR、defStyle);
setPositiveButtonText(“Set”);
setNegativeButtonText(“取消”);
}
@凌驾
受保护的视图onCreateDialogView(){
字符串[]nums=新字符串[61];
对于(int i=0;i b){
ret=添加和检查(b、a、msg);
}否则{
if(a<0){
if(b<0){
if(Long.MIN_VALUE-b欢迎使用SO..您尝试了什么..?我尝试在一个类中设置两个不同的微调器和两个不同的日期选择器以及微调器…因为我想分别设置开始事件日期和结束事件日期。您能帮我设置日期选择器吗?事实上,我在android中是新手。。!!
<?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>