1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package android.support.v14.preference; 18 19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21 import android.os.Bundle; 22 import android.support.annotation.NonNull; 23 import android.support.annotation.RestrictTo; 24 import android.support.v7.preference.EditTextPreference; 25 import android.view.View; 26 import android.widget.EditText; 27 28 public class EditTextPreferenceDialogFragment extends PreferenceDialogFragment { 29 30 private static final String SAVE_STATE_TEXT = "EditTextPreferenceDialogFragment.text"; 31 32 private EditText mEditText; 33 34 private CharSequence mText; 35 newInstance(String key)36 public static EditTextPreferenceDialogFragment newInstance(String key) { 37 final EditTextPreferenceDialogFragment 38 fragment = new EditTextPreferenceDialogFragment(); 39 final Bundle b = new Bundle(1); 40 b.putString(ARG_KEY, key); 41 fragment.setArguments(b); 42 return fragment; 43 } 44 45 @Override onCreate(Bundle savedInstanceState)46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 if (savedInstanceState == null) { 49 mText = getEditTextPreference().getText(); 50 } else { 51 mText = savedInstanceState.getCharSequence(SAVE_STATE_TEXT); 52 } 53 } 54 55 @Override onSaveInstanceState(@onNull Bundle outState)56 public void onSaveInstanceState(@NonNull Bundle outState) { 57 super.onSaveInstanceState(outState); 58 outState.putCharSequence(SAVE_STATE_TEXT, mText); 59 } 60 61 @Override onBindDialogView(View view)62 protected void onBindDialogView(View view) { 63 super.onBindDialogView(view); 64 65 mEditText = (EditText) view.findViewById(android.R.id.edit); 66 67 if (mEditText == null) { 68 throw new IllegalStateException("Dialog view must contain an EditText with id" + 69 " @android:id/edit"); 70 } 71 72 mEditText.setText(mText); 73 } 74 getEditTextPreference()75 private EditTextPreference getEditTextPreference() { 76 return (EditTextPreference) getPreference(); 77 } 78 79 /** @hide */ 80 @RestrictTo(LIBRARY_GROUP) 81 @Override needInputMethod()82 protected boolean needInputMethod() { 83 // We want the input method to show, if possible, when dialog is displayed 84 return true; 85 } 86 87 @Override onDialogClosed(boolean positiveResult)88 public void onDialogClosed(boolean positiveResult) { 89 90 if (positiveResult) { 91 String value = mEditText.getText().toString(); 92 if (getEditTextPreference().callChangeListener(value)) { 93 getEditTextPreference().setText(value); 94 } 95 } 96 } 97 98 } 99