另一种全局接收方式已更新:https://blog.csdn.net/qq_39731011/article/details/107684794 (Activity事件全局分发)

扫码盒子通用接收原理

1.本质上是键盘输入事件

2.可以响应EditText的输入事件(前提是获得焦点)

首先写接收源: (假设在MainActivity接收付款码)

接收代码:

public ScanKeyManager scanKeyManager;    private void initView(){        EditText edt = findViewById(R.id.edit_pay_code);        edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {            @Override            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                String str = v.getText().toString().trim();                Log.e("付款码:", str + "");                if (str.length() == 18) {//134609522362925502                    v.setText("");                    if (scanKeyManager != null)                        scanKeyManager.analysisKeyEvent(str);                }                closeKeyboard();  //事件结束后收回键盘                return true;            }        });    }    private void closeKeyboard(){        if(edt!=null) {            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);            inputMethodManager.hideSoftInputFromWindow(edt.getWindowToken(), 0);        }    }    public void requestEditText(){        if(edt!=null){            edt.setFocusable(true);            edt.setFocusableInTouchMode(true);            edt.requestFocus();        }    }
/* *作者:赵星海 *时间:2020/5/27 16:22 *用途: 扫码盒子接收回调 */ public class ScanKeyManager {    public OnScanValueListener mListener;    public interface OnScanValueListener {        void onScanValue(String value);    }    public ScanKeyManager(OnScanValueListener listener) {        this.mListener = listener;    }    public void analysisKeyEvent(String str) {        mListener.onScanValue(str+"");    }    }

当你要开启扫码接收的时候:(假设在PopupWindow中获得付款码)

            ((MainActivity) mContext).requestEditText();            Toast.makeText(mContext, "请出示付款码", Toast.LENGTH_SHORT).show();            // 语音提醒            SpeechUtils.getInstance(mContext).speakText("请出示付款码");            ((MainActivity) mContext).scanKeyManager = new ScanKeyManager(new ScanKeyManager.OnScanValueListener() {                    @Override                    public void onScanValue(String value) {                        // Log.e("扫码结果:",""+value);                        // Toast.makeText(mContext,"结果"+value,Toast.LENGTH_SHORT).show();                        if (value == null) return;                        if (value.trim().substring(0, 2).equals("28")) {                            goPay(2, value);//1微信 2支付宝                         } else {                            goPay(1, value);                        }                    }            });

 

付款完成后,需要关闭接收。

((MainActivity) mContext).scanKeyManager = null;

完事,就是这么简单,需要额外注意一点:

如果是在PopupWindow中开启回调的话,由于PopupWindow盖在Activity之上,抢了Activity上EditText的焦点。

所以需要在配置PopupWindow时,要将这个配置改成false:this.setFocusable(false); 

或者注释掉这行代码,因为它默认是false。

    /**     * 设置窗口的相关属性     */    @SuppressLint("InlinedApi")    private void setPopupWindow() {        this.setContentView(mPopView);// 设置View        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的高        // this.setFocusable(true);// 设置弹出窗口可获得焦点    }
 

更多相关文章

  1. Android零基础入门第33节:Android事件处理概述
  2. Android 模拟MotionEvent事件 触发输入法
  3. Android取消EditText自动获取焦点默认行为
  4. android fragment ontouch 事件
  5. Android之Input子系统事件分发流程
  6. Android 在xml布局配置文件中给Button按钮添加事件
  7. Android事件分发机制详解
  8. Android Input事件APP端流程分析
  9. android noTouch 事件

随机推荐

  1. roboguice - Android上的依赖注入框架
  2. LinearLayout中android:layout_gravity="
  3. android开源库合集
  4. android 系统裁剪
  5. Android二维码开源项目zxing用例简化和生
  6. Notification用法,注意API版本,API大于11的
  7. Android的标题栏
  8. Android(安卓)SVG和Lottie库播放动画(Json
  9. Android SDK下载地址
  10. AMD windows7安装和打开genymotion、andr