Android 系统使用蓝牙遥控器的确定键无法调起输入法问题解决方案

测试反馈Android蓝牙遥控器适配问题:瞩目应用中,点击加入会议,使用遥控器点击返回键收起输入法键盘,再点击会议名称栏无法调出输入法键盘。此问题在Android 6.0必现。

经查看分析,应用使用了AutoCompleteTextView控件,此控件自动添加了onclicklistener监听事件。
AutoCompleteTextView继承了EditText,EditText的父类是TextView。蓝牙遥控器的确定按键对应的Android keycode事件是KeyEvent.KEYCODE_DPAD_CENTER。之后查看TextView类得知,Android 6.0系统在设有onclicklistener监听的输入框时,遥控器确定键按下默认不调起输入法。

所以只需修改TextView类的onKeyUp方法,在收到KEYCODE_DPAD_CENTER时,放开hasonClickListener判断,即可直接调起输入法。
文件位置: /frameworks/base/core/java/android/widget/TextView.java
修改如下:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (!isEnabled()) {
    return super.onKeyUp(keyCode, event);
}

if (!KeyEvent.isModifierKey(keyCode)) {
    mPreventDefaultMovement = false;
}

switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_CENTER:
        if (event.hasNoModifiers()) {
            /*
             * If there is a click listener, just call through to
             * super, which will invoke it.
             *
             * If there isn't a click listener, try to show the soft
             * input method.  (It will also
             * call performClick(), but that won't do anything in
             * this case.)
             */
            ////add by pengtg for ROM begin.
            //if (!hasOnClickListeners()) {
                if (mMovement != null && mText instanceof Editable
                        && mLayout != null && onCheckIsTextEditor()) {
                    InputMethodManager imm = InputMethodManager.peekInstance();
                    viewClicked(imm);
                    if (imm != null && getShowSoftInputOnFocus()) {
                        imm.showSoftInput(this, 0);
                    }
                }
            //}
            ////add by pengtg for ROM end.
        }
        return super.onKeyUp(keyCode, event);
        ......
    }
    ......
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现一个基于Android蓝牙遥控器应用,需要完成以下几个步骤: 1. 在应用中添加UI元素,如按钮、滑块等,用于控制遥控器的功能; 2. 在应用中实现蓝牙连接功能,包括搜索设备、建立连接等; 3. 在应用中实现蓝牙数据传输功能,包括向蓝牙设备发送数据和接收蓝牙设备发送的数据; 4. 根据具体需求,将UI元素和蓝牙数据传输功能进行绑定,实现遥控器的功能。 下面是一个简单的示例代码,演示了如何在应用中实现蓝牙遥控器的功能: ```java public class MainActivity extends AppCompatActivity { private static final String TAG = "BluetoothControl"; private BluetoothAdapter mBluetoothAdapter; private BluetoothDevice mBluetoothDevice; private BluetoothSocket mBluetoothSocket; private OutputStream mOutputStream; private Button mButtonUp; private Button mButtonDown; private Button mButtonLeft; private Button mButtonRight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取BluetoothAdapter实例 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 获取UI元素 mButtonUp = findViewById(R.id.button_up); mButtonDown = findViewById(R.id.button_down); mButtonLeft = findViewById(R.id.button_left); mButtonRight = findViewById(R.id.button_right); // 设置UI元素的点击事件 mButtonUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("UP"); } }); mButtonDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("DOWN"); } }); mButtonLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("LEFT"); } }); mButtonRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("RIGHT"); } }); } // 搜索蓝牙设备 private void searchBluetoothDevice() { if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } mBluetoothAdapter.startDiscovery(); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if ("蓝牙设备名称".equals(device.getName())) { mBluetoothAdapter.cancelDiscovery(); mBluetoothDevice = device; connectBluetoothDevice(); } } } }; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter); } // 建立蓝牙连接 private void connectBluetoothDevice() { try { UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid); mBluetoothSocket.connect(); mOutputStream = mBluetoothSocket.getOutputStream(); } catch (IOException e) { Log.e(TAG, "connectBluetoothDevice: ", e); } } // 向蓝牙设备发送数据 private void sendBluetoothData(String data) { try { mOutputStream.write(data.getBytes()); } catch (IOException e) { Log.e(TAG, "sendBluetoothData: ", e); } } @Override protected void onDestroy() { super.onDestroy(); try { mBluetoothSocket.close(); } catch (IOException e) { Log.e(TAG, "onDestroy: ", e); } } } ``` 需要注意的是,上述示例代码仅适用于演示如何实现蓝牙遥控器的功能,实际应用中需要根据具体需求进行修改和完善。同时,还需要考虑蓝牙连接和数据传输过程中可能出现的异常情况,并进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值