|
24 | 24 | import android.view.KeyEvent; |
25 | 25 | import android.view.Menu; |
26 | 26 | import android.view.MenuItem; |
| 27 | +import android.view.View; |
27 | 28 | import android.view.inputmethod.EditorInfo; |
28 | 29 | import android.view.inputmethod.InputMethodManager; |
29 | 30 | import android.widget.LinearLayout; |
@@ -100,6 +101,31 @@ public VariableTextInput(Context context) { |
100 | 101 | editText.setBackground(reactViewBackgroundDrawable); |
101 | 102 | scrollView.addView(editText); |
102 | 103 | this.addView(scrollView); |
| 104 | + //添加键盘onFocusChangeListener监听器 |
| 105 | + editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { |
| 106 | + @Override |
| 107 | + public void onFocusChange(View v, boolean hasFocus) { |
| 108 | + if (hasFocus) { |
| 109 | + // 当 EditText 获取焦点时执行的操作 |
| 110 | + // 例如:显示键盘 |
| 111 | + WritableMap event = Arguments.createMap(); |
| 112 | + event.putString("text", editText.getText().toString()); |
| 113 | + final Context context = getContext(); |
| 114 | + if (context instanceof ReactContext) { |
| 115 | + ((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidFocus", event); |
| 116 | + } |
| 117 | + } else { |
| 118 | + // 当 EditText 失去焦点时执行的操作 |
| 119 | + // 例如:隐藏键盘 |
| 120 | + WritableMap event = Arguments.createMap(); |
| 121 | + event.putString("text", editText.getText().toString()); |
| 122 | + final Context context = getContext(); |
| 123 | + if (context instanceof ReactContext) { |
| 124 | + ((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidBlur", event); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
103 | 129 | // 添加 TextWatcher 监听器 |
104 | 130 | editText.addTextChangedListener(new TextWatcher() { |
105 | 131 | private int oldHeight = editText.getHeight(); // 保存旧的高度 |
|
0 commit comments