android 模擬輸入框edittext控件按下回車或掃描頭掃描到條碼

edtScan.setText(result); 
edtScan.onEditorAction(EditorInfo.IME_ACTION_NEXT);

場景:PDA都有掃描頭,但有時想用本身的手機來掃碼。因而給項目添加了相機使用Zbar識別二維碼和條碼的功能,
如今掃描頭掃描到二維碼或條碼時是在PDA的掃描設置里加後綴(回車+換行)來實現輸入框焦點跳轉觸發的。
相機識別二維碼後須要模擬掃描頭的掃描動做。不須要加回車換行,親試無用,發現使用EditorInfo.IME_ACTION_NEXT,讓輸入框切換到下一個焦點便可。
 edtScan.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                String scanertext = edtScan.getText().toString();
                if (scanertext.contains("-") && scanertext.length() > 3) {
                    edtScan.setText("");
                    RefreshWebView(scanertext);
                } else if (scanertext.length() > 0) {
                    edtScan.setText("");
                    String msg = "編碼規則不符,請掃描位置二維碼!";
                    Toast.makeText(HtmlViewActivity.this, msg,
                            Toast.LENGTH_SHORT).show();
                    MainActivity.getInstance().SpeakVoice(msg);
                    String head = "<head>"
                            + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                            + "<style>html{padding:15px;} body{word-wrap:break-word;font-size:13px;padding:0px;margin:0px} p{padding:0px;margin:0px;font-size:13px;color:#222222;line-height:1.3;} img{padding:0px,margin:0px;max-width:100%; width:auto; height:auto;}</style>"
                            + "</head>";
                    webview.loadData("<html>" + head + "<body>"
                                    + " <h1>請掃描位置二維碼!eg:A-R1:C1</h1> " + "</body></html>",
                            "text/html;charset=utf-8", "utf-8");
                }
                return true;
            }
        });
    }
相關文章
相關標籤/搜索