| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License |
| */ |
| |
| package android.support.v7.preference; |
| |
| import android.os.Bundle; |
| import android.view.View; |
| import android.view.ViewGroup; |
| import android.view.ViewParent; |
| import android.widget.EditText; |
| |
| public class EditTextPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat { |
| |
| private EditText mEditText; |
| |
| public static EditTextPreferenceDialogFragmentCompat newInstance(String key) { |
| final EditTextPreferenceDialogFragmentCompat |
| fragment = new EditTextPreferenceDialogFragmentCompat(); |
| final Bundle b = new Bundle(1); |
| b.putString(ARG_KEY, key); |
| fragment.setArguments(b); |
| return fragment; |
| } |
| |
| @Override |
| protected void onBindDialogView(View view) { |
| super.onBindDialogView(view); |
| |
| mEditText = new EditText(view.getContext()); |
| // Give it an ID so it can be saved/restored |
| mEditText.setId(android.R.id.edit); |
| |
| mEditText.setText(getEditTextPreference().getText()); |
| |
| ViewParent oldParent = mEditText.getParent(); |
| if (oldParent != view) { |
| if (oldParent != null) { |
| ((ViewGroup) oldParent).removeView(mEditText); |
| } |
| onAddEditTextToDialogView(view, mEditText); |
| } |
| } |
| |
| private EditTextPreference getEditTextPreference() { |
| return (EditTextPreference) getPreference(); |
| } |
| |
| /** @hide */ |
| @Override |
| protected boolean needInputMethod() { |
| // We want the input method to show, if possible, when dialog is displayed |
| return true; |
| } |
| |
| /** |
| * Adds the EditText widget of this preference to the dialog's view. |
| * |
| * @param dialogView The dialog view. |
| */ |
| protected void onAddEditTextToDialogView(View dialogView, EditText editText) { |
| ViewGroup container = (ViewGroup) dialogView |
| .findViewById(R.id.edittext_container); |
| if (container != null) { |
| container.addView(editText, ViewGroup.LayoutParams.FILL_PARENT, |
| ViewGroup.LayoutParams.WRAP_CONTENT); |
| } |
| } |
| |
| @Override |
| public void onDialogClosed(boolean positiveResult) { |
| |
| if (positiveResult) { |
| String value = mEditText.getText().toString(); |
| if (getEditTextPreference().callChangeListener(value)) { |
| getEditTextPreference().setText(value); |
| } |
| } |
| } |
| |
| } |