软键盘输入后edittext无法显示字

这篇博客探讨了在Android开发中遇到的PopupWindow遮挡输入法的问题,通过设置PopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED)和PopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)来解决此问题。作者提醒开发者这两个API的使用可能带来的坑,帮助他们避免不必要的困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); //解决遮盖输入法
//                PopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

这两句代码的错神坑!!!!!!

### 创建并配置自定义数字软键盘 为了在 Android 中为 `EditText` 配置自定义数字软键盘,可以按照如下方式操作: #### 实现 InputMethodService 类 创建一个新的类继承自 `InputMethodService` 并重写相应的方法。这一步骤是为了构建一个完整的输入法框架。 ```java public class CustomNumberKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView kv; private Keyboard keyboard; @Override public View onCreateInputView() { kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard_view, null); keyboard = new Keyboard(this, R.xml.number_keyboard); kv.setKeyboard(keyboard); kv.setOnKeyboardActionListener(this); return kv; } // 处理按键事件 @Override public void onKey(int primaryCode, int[] keyCodes) { long eventTime = System.currentTimeMillis(); KeyEvent ev = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, primaryCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0); getCurrentInputConnection().sendDownUpKeyEvent(ev); } } ``` 此部分代码实现了基本的输入法服务功能,并设置了监听器用于处理键入事件[^1]。 #### 定义 XML 键盘布局资源文件 在 res/xml 文件夹下新建名为 number_keyboard.xml 的文件来描述键盘的具体样式和结构。 ```xml <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="33%p" android:keyHeight="60dp"> <!-- 数字按钮 --> <Row> <Key android:codes="7" android:keyLabel="1"/> <Key android:codes="8" android:keyLabel="2"/> <Key android:codes="9" android:keyLabel="3"/> </Row> <Row> <Key android:codes="4" android:keyLabel="4"/> <Key android:codes="5" android:keyLabel="5"/> <Key android:codes="6" android:keyLabel="6"/> </Row> <Row> <Key android:codes="0" android:keyLabel="7"/> <Key android:codes="1" android:keyLabel="8"/> <Key android:codes="2" android:keyLabel="9"/> </Row> <Row> <Key android:codes="-5" android:keyIcon="@drawable/ic_backspace_white_24dp"/> <Key android:codes="3" android:keyLabel="0"/> <Key android:codes="-4" android:keyIcon="@drawable/ic_done_white_24dp"/> </Row> </Keyboard> ``` 这段XML定义了一个简单的三行四列带删除和确认键的数字键盘[^2]。 #### 修改清单文件注册输入法组件 为了让应用程序能够识别新的输入法模块,在应用项目的 `AndroidManifest.xml` 添加以下声明: ```xml <input-method xmlns:android="http://schemas.android.com/apk/res/android" android:name=".CustomNumberKeyboard"/> <!-- 或者 --> <service android:name=".CustomNumberKeyboard" android:permission="android.permission.BIND_INPUT_METHOD"> <intent-filter> <action android:name="android.view.InputMethod"/> </intent-filter> <meta-data android:name="android.view.im" android:resource="@xml/method"/> </service> ``` 这样就完成了对新输入法的服务端登记工作[^3]。 #### 设置 EditText 使用特定输入法 最后要让指定的 `EditText` 组件使用这个定制化的输入法作为默认选项,则可以在 Activity 启动时通过编程的方式切换当前使用的 IME(Input Method Editor): ```java // 获取系统的输入法管理器实例 final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // 找到目标编辑框控件 EditText editText = findViewById(R.id.editText); editText.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ imm.showSoftInput(editText, 0); Handler handler=new Handler(); handler.postDelayed(new Runnable(){ @Override public void run() { imm.setInputMethod(editText.getWindowToken(), "com.example.customkeyboard/.CustomNumberKeyboard"); } }, 200L); } }); ``` 以上就是整个流程的大致说明,需要注意的是实际开发过程中可能还需要考虑更多细节上的优化以及兼容性问题[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值