FragmentDialog的错误行为与TimePicker

问题描述 投票:1回答:1

我创建自定义视图的对话框。一个视图中的所述部件的是TimePicker。如果这样做,确定+取消TimePicker的按钮,采取控制和FragmentDialog默认按钮是禁用。

这导致一个问题,因为根据我展示/用户数据隐藏TimePicker视图,当它被隐藏也隐藏OK +取消按钮。

有一个自定义视图是很重要的,这样用户就可以在一个视图中选择所有相关数据。

编辑 - 添加图片,显示问题

enter image description here

layout.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="1000dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:text="@string/automatic_backup_type"
                android:textColor="@color/dialog_text_color"/>

            <!-- depending on the selected value here the timePart layout is showed or hide -->
            <Spinner
                android:id="@+id/automaticBackupType"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:background="@drawable/dialog_spinner_background"
                android:prompt="@string/automatic_backup_type"
                android:textColor="@color/dialog_text_color"
                android:spinnerMode="dropdown"/>
        </LinearLayout>

<!-- Some more controls here that are not relevant -->

        <LinearLayout
            android:id="@+id/timePart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/automatic_backup_time"
                android:textColor="@color/dialog_text_color"/>

            <TimePicker
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

为了显示我使用的代码对话框:

new AutomaticBackupConfigurationAlert().show(getSupportFragmentManager(), "TAG");

自动备份配置警报代码:

public static class AutomaticBackupConfigurationAlert extends DialogFragment {

    private Spinner    automaticBackupType;
    private View       timePart;
    private TimePicker time;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AutomaticBackupType type;
        Calendar time;

        if (savedInstanceState != null) {
            type = (AutomaticBackupType)savedInstanceState.getSerializable(KEY_TYPE);
            time = (Calendar)savedInstanceState.getSerializable(KEY_TIME);
        } else {
            type = SettingsHelper.getAutomaticBackupType();
            time = SettingsHelper.getAutomaticBackupTime();
        }

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        @SuppressLint("InflateParams")
        View view = inflater.inflate(R.layout.dialog_automatic_backup, null);

        this.automaticBackupType = view.findViewById(R.id.automaticBackupType);
        this.timePart = view.findViewById(R.id.timePart); // When this is hide, no buttons for OK+Cancel
        this.time = view.findViewById(R.id.time);

        updateVisibility(type);

        {
            // backup type
            ArrayAdapter<AutomaticBackupType> adapter = new ArrayAdapter<>(getActivity(), R.layout.dialog_spinner_item, AutomaticBackupType.values());
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
            automaticBackupType.setAdapter(adapter);
            automaticBackupType.setSelection(type.ordinal());
        }
        {
            // time
            this.time.setIs24HourView(DateFormat.is24HourFormat(getActivity()));
            this.time.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));
            this.time.setCurrentMinute(time.get(Calendar.MINUTE));
        }

        automaticBackupType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                AutomaticBackupType tmpAutomaticBackupType = (AutomaticBackupType)automaticBackupType.getSelectedItem();
                updateVisibility(tmpAutomaticBackupType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.automatic_backup_configuration_title)
                .setView(view)
                .setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do nothing here because we override this button later to change the close behaviour.
                        // However, we still need this because on older versions of Android unless we
                        // pass a handler the button doesn't get instantiated
                    }
                })
                .setNegativeButton(R.string.btn_cancel, null)
                .create();
    }

    @Override
    public void onStart() {
        super.onStart();
        AlertDialog d = (AlertDialog)getDialog();
        if (d != null) {
            Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(v -> {
                // save the data
            });
        }
    }

    private void updateVisibility(AutomaticBackupType tmpAutomaticBackupType) {
        if (tmpAutomaticBackupType != null) {
            UiHelper.setVisibility(timePart, !tmpAutomaticBackupType.equals(AutomaticBackupType.disabled), true);
        }
    }
}
android timepicker
1个回答
0
投票

只要找到了解决方法(我不喜欢它,但它的作品)。

我的情况下,别人会需要它在这里张贴。

看来,TimePicker有正/自然/负与相同的ID AlertDialog按钮。当我设定他们使用对话框生成器,它实际上是设置一个在TimePicker。

在一般情况下,解决方案是一个视图添加到对话框而不TimePicker视图,并将其添加的按钮被设置后进行。

该代码以两种位置的上方改变:首先在时间设置块onCreateDialog方法:

{
    // time
    this.time.setIs24HourView(DateFormat.is24HourFormat(getActivity()));
    this.time.setCurrentHour(time.get(Calendar.HOUR_OF_DAY));
    this.time.setCurrentMinute(time.get(Calendar.MINUTE));
    timePart.removeView(this.time); // remove the TimePicker view from the layout
}

其次在OnStart方法

public void onStart() {
    super.onStart();
    AlertDialog d = (AlertDialog)getDialog();
    if (d != null) {
        Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
        timePart.addView(time); // Now we can add the view
        positiveButton.setOnClickListener(v -> {
            // save the data
        });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.