| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.util.AttributeSet; |
| 21 |
import android.view.View; |
| 22 |
import android.view.inputmethod.EditorInfo; |
| 23 |
import android.view.inputmethod.InputConnection; |
| 24 |
import android.view.inputmethod.InputMethodManager; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class ImeAwareEditText extends EditText { |
| 32 |
private boolean mHasPendingShowSoftInputRequest; |
| 33 |
final Runnable mRunShowSoftInputIfNecessary = () -> showSoftInputIfNecessary(); |
| 34 |
|
| 35 |
public ImeAwareEditText(Context context) { |
| 36 |
super(context, null); |
| 37 |
} |
| 38 |
|
| 39 |
public ImeAwareEditText(Context context, AttributeSet attrs) { |
| 40 |
super(context, attrs); |
| 41 |
} |
| 42 |
|
| 43 |
public ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
| 44 |
super(context, attrs, defStyleAttr); |
| 45 |
} |
| 46 |
|
| 47 |
public ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr, |
| 48 |
int defStyleRes) { |
| 49 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@Override |
| 63 |
public InputConnection onCreateInputConnection(EditorInfo editorInfo) { |
| 64 |
final InputConnection ic = super.onCreateInputConnection(editorInfo); |
| 65 |
if (mHasPendingShowSoftInputRequest) { |
| 66 |
removeCallbacks(mRunShowSoftInputIfNecessary); |
| 67 |
post(mRunShowSoftInputIfNecessary); |
| 68 |
} |
| 69 |
return ic; |
| 70 |
} |
| 71 |
|
| 72 |
private void showSoftInputIfNecessary() { |
| 73 |
if (mHasPendingShowSoftInputRequest) { |
| 74 |
final InputMethodManager imm = |
| 75 |
getContext().getSystemService(InputMethodManager.class); |
| 76 |
imm.showSoftInput(this, 0); |
| 77 |
mHasPendingShowSoftInputRequest = false; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public void scheduleShowSoftInput() { |
| 82 |
final InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
| 83 |
if (imm.hasActiveInputConnection(this)) { |
| 84 |
|
| 85 |
|
| 86 |
mHasPendingShowSoftInputRequest = false; |
| 87 |
removeCallbacks(mRunShowSoftInputIfNecessary); |
| 88 |
imm.showSoftInput(this, 0); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
mHasPendingShowSoftInputRequest = true; |
| 95 |
} |
| 96 |
} |