Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 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 com.android.contacts; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Rect; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.inputmethod.InputMethodManager; |
| 23 | import android.widget.EditText; |
| 24 | |
| 25 | /** |
| 26 | * A custom text editor that optionally automatically brings up the soft |
| 27 | * keyboard when first focused. |
| 28 | */ |
| 29 | public class SearchEditText extends EditText { |
| 30 | |
| 31 | private boolean mAutoShowKeyboard; |
| 32 | |
| 33 | public SearchEditText(Context context, AttributeSet attrs) { |
| 34 | super(context, attrs); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Automatically show the soft keyboard when the field gets focus. This is a |
| 39 | * single-shot setting - it is reset as soon as the keyboard is shown. |
| 40 | */ |
| 41 | public void setAutoShowKeyboard(boolean flag) { |
| 42 | mAutoShowKeyboard = flag; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 47 | super.onWindowFocusChanged(hasWindowFocus); |
| 48 | if (hasWindowFocus && mAutoShowKeyboard) { |
| 49 | showKeyboard(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 55 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 56 | if (focused && mAutoShowKeyboard) { |
| 57 | showKeyboard(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Explicitly brings up the soft keyboard if necessary. |
| 63 | */ |
| 64 | private void showKeyboard() { |
| 65 | InputMethodManager inputManager = (InputMethodManager)getContext().getSystemService( |
| 66 | Context.INPUT_METHOD_SERVICE); |
| 67 | inputManager.showSoftInput(this, 0); |
| 68 | mAutoShowKeyboard = false; |
| 69 | } |
| 70 | |
| 71 | public void hideKeyboard() { |
| 72 | InputMethodManager inputManager = (InputMethodManager)getContext().getSystemService( |
| 73 | Context.INPUT_METHOD_SERVICE); |
| 74 | inputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 75 | } |
| 76 | } |