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; } }); }