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.v7.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.view.View; 25 import android.widget.EditText; 26 27 public class EditTextPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat { 28 29 private static final String SAVE_STATE_TEXT = "EditTextPreferenceDialogFragment.text"; 30 31 private EditText mEditText; 32 33 private CharSequence mText; 34 newInstance(String key)35 public static EditTextPreferenceDialogFragmentCompat newInstance(String key) { 36 final EditTextPreferenceDialogFragmentCompat 37 fragment = new EditTextPreferenceDialogFragmentCompat(); 38 final Bundle b = new Bundle(1); 39 b.putString(ARG_KEY, key); 40 fragment.setArguments(b); 41 return fragment; 42 } 43 44 @Override onCreate(Bundle savedInstanceState)45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 if (savedInstanceState == null) { 48 mText = getEditTextPreference().getText(); 49 } else { 50 mText = savedInstanceState.getCharSequence(SAVE_STATE_TEXT); 51 } 52 } 53 54 @Override onSaveInstanceState(@onNull Bundle outState)55 public void onSaveInstanceState(@NonNull Bundle outState) { 56 super.onSaveInstanceState(outState); 57 outState.putCharSequence(SAVE_STATE_TEXT, mText); 58 } 59 60 @Override onBindDialogView(View view)61 protected void onBindDialogView(View view) { 62 super.onBindDialogView(view); 63 64 mEditText = (EditText) view.findViewById(android.R.id.edit); 65 66 if (mEditText == null) { 67 throw new IllegalStateException("Dialog view must contain an EditText with id" + 68 " @android:id/edit"); 69 } 70 71 mEditText.setText(mText); 72 } 73 getEditTextPreference()74 private EditTextPreference getEditTextPreference() { 75 return (EditTextPreference) getPreference(); 76 } 77 78 /** @hide */ 79 @RestrictTo(LIBRARY_GROUP) 80 @Override needInputMethod()81 protected boolean needInputMethod() { 82 // We want the input method to show, if possible, when dialog is displayed 83 return true; 84 } 85 86 @Override onDialogClosed(boolean positiveResult)87 public void onDialogClosed(boolean positiveResult) { 88 89 if (positiveResult) { 90 String value = mEditText.getText().toString(); 91 if (getEditTextPreference().callChangeListener(value)) { 92 getEditTextPreference().setText(value); 93 } 94 } 95 } 96 97 } 98