简体   繁体   中英

Extend EditTextPreference and display the DialogMessage

I have extended EditTextPreference, but the Dialog Message won't display. This happens if I add the dialogMessage programatically or in the the preferences.xml.

Here is my onBindDialogView:

AutoCompleteTextView editText = mEditText;
editText.setText(getText());

ViewParent oldParent = editText.getParent();
if (oldParent != view) {
    if (oldParent != null) {
        ((ViewGroup) oldParent).removeView(editText);
    }
    onAddEditTextToDialogView(view, editText);
}

Is the dialog message really absent? It's probably there but its text color might make it less (or not) visible. (Or try to dismiss software keyboard). Try experimenting with dialog messages having a number of "\\n" characters and see if that affects dialog layout. If so, it means the dialog message is actually there but camouflaged too well.

EditTextPreference brings a text view (in the preference_dialog_edittext.xml) that replaces the existing one (in the alert_dialog.xml) for the dialog message, but unfortunately with different text style, which might cause a visibility problem under certain themes. Even their sizes are different.

One solution might be to obtain the text color and size from the original text view to be replaced and apply them to the new one, but I would suggest retaining the original text view instead, because it's more likely to be visually consistent if there are any future UI changes. Try adding the following overrides

protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);
    builder.setMessage(getDialogMessage()); // bring back the original text view
}

protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
    int id = getContext().getResources().getIdentifier("edittext_container", "id", "android");
    ViewGroup container = (ViewGroup) dialogView.findViewById(id);
    container.removeAllViews(); // remove the new text view
    super.onAddEditTextToDialogView(dialogView, editText);
}

If you think the dialog message and the edittext view is too far apart, they can be brought together a little closer by adding another override:

protected void showDialog(Bundle state) {
    super.showDialog(state);
    int id = getContext().getResources().getIdentifier("message", "id", "android");
    TextView message = (TextView) getDialog().findViewById(id);
    message.setPadding(message.getPaddingLeft(), message.getPaddingTop(), message.getPaddingRight(), 0);
}

and add the following line in the onAddEditTextToDialogView method after calling removeAllViews:

    container.setPadding(container.getPaddingLeft(), 0, container.getPaddingRight(), container.getPaddingBottom());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM