gpt4 book ai didi

android - 在 Android 中创建自定义对话监听器

转载 作者:行者123 更新时间:2023-11-30 02:04:52 25 4
gpt4 key购买 nike

我想通过使用数字选择器在 Android 中创建一个自定义时间选择器对话框。我将如何实现监听器?

下面是我的代码 fragment :

    intervalEditText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DurationPickerDialog durationPickerDialog = new DurationPickerDialog(getActivity(),
new DurationPickerDialog.onDurationSetlistener() {

@Override
public void onDurationSet(View v, int hour, int minute, int seconds) {
intervalEditText.setText(hour + ":" + minute + ":" + seconds);
}
});
durationPickerDialog.show();
}
});

DurationPickerDialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:divider="@drawable/horizontal_divider_red"
android:gravity="center"
android:orientation="vertical"
android:showDividers="middle">

<com.identiv.utrust.sense.uielements.FLightTextView
android:id="@+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="@color/labelSetupText"
android:text="Interval: 00:00:00"
android:textSize="20sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:gravity="center"
android:orientation="horizontal">

<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
android:focusable="false"
android:gravity="center" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":" />

<NumberPicker
android:id="@+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:focusable="false"
android:gravity="center" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":" />

<NumberPicker
android:id="@+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="24dp"
android:layout_marginTop="6dp"
android:focusable="false"
android:gravity="center" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/vertical_divider_red"
android:orientation="horizontal"
android:showDividers="middle">

<com.identiv.utrust.sense.uielements.FRomanButton
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="Cancel"
android:textColor="@color/labelSetupText"
android:textSize="14sp" />

<com.identiv.utrust.sense.uielements.FRomanButton
android:id="@+id/set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="Set"
android:textColor="@color/labelSetupText"
android:textSize="14sp" />

</LinearLayout>

</LinearLayout>

DurationPickerDialog.java

public class DurationPickerDialog extends Dialog {

private static NumberPicker hourPicker;
private static NumberPicker minutesPicker;
private static NumberPicker secondsPicker;
private static Button cancelButton;
private static Button setButton;
private static TextView duration;

private onDurationSetlistener listener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.datepickerdialog);

hourPicker = (NumberPicker)findViewById(R.id.numberPicker1);
minutesPicker = (NumberPicker)findViewById(R.id.numberPicker2);
secondsPicker = (NumberPicker)findViewById(R.id.numberPicker3);
cancelButton = (Button)findViewById(R.id.cancel);
setButton = (Button)findViewById(R.id.set);
duration = (TextView)findViewById(R.id.duration);

hourPicker.setMaxValue(9);
hourPicker.setMinValue(0);
setDividerColor(hourPicker);
hourPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

minutesPicker.setMaxValue(59);
minutesPicker.setMinValue(0);
setDividerColor(minutesPicker);
minutesPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

secondsPicker.setMaxValue(59);
secondsPicker.setMinValue(0);
setDividerColor(secondsPicker);
secondsPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});

setButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDurationSet(v, hourPicker.getValue(), minutesPicker.getValue(), secondsPicker.getValue());
}
});

hourPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
duration.setText("Interval: " + String.format("%02d", hourPicker.getValue()) + ":" + String.format("%02d", minutesPicker.getValue()) + ":" + String.format("%02d", secondsPicker.getValue()));
}
});

minutesPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
duration.setText("Interval: " + String.format("%02d", hourPicker.getValue()) + ":" + String.format("%02d", minutesPicker.getValue()) + ":" + String.format("%02d", secondsPicker.getValue()));
}
});

secondsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
duration.setText("Interval: " + String.format("%02d", hourPicker.getValue()) + ":" + String.format("%02d", minutesPicker.getValue()) + ":" + String.format("%02d", secondsPicker.getValue()));
}
});

}

private void setDividerColor(NumberPicker picker) {

java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try {
pf.set(picker, getContext().getResources().getDrawable(R.drawable.horizontal_divider_red));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}

private void updateTitle(int hour, int min, int sec){
duration.setText("Duration: " + hour + ":" + min + ":" + sec);
}

public DurationPickerDialog(Context context, onDurationSetlistener callBack) {
super(context);
}

public static interface onDurationSetlistener {
public abstract void onDurationSet (View v, int hour, int minute, int seconds);
}
}

上面的代码导致应用程序在单击设置时崩溃。我想知道我在创建自定义对话框时犯了什么错误。我希望将对话中的时间传递给 Activity 。

LogcatReport

Process: com.identiv.utrust.sense.temperaturetracker, PID: 27790
java.lang.NullPointerException
at com.identiv.utrust.sense.uielements.DurationPickerDialog$2.onClick(DurationPickerDialog.java:71)
at android.view.View.performClick(View.java:4710)
at android.view.View$PerformClick.run(View.java:19510)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5678)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

我发现 onDurationListener 对象为空。我的实现是否正确?

最佳答案

这是您获得 NullPointerException

的行
listener.onDurationSet(v, hourPicker.getValue(), minutesPicker.getValue(), secondsPicker.getValue());

因为 listener 是空的,所以你还没有在任何地方初始化它。

关于android - 在 Android 中创建自定义对话监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30838955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com