自定义键盘扫描条形码并将结果插入EditText

时间:2017-09-26 21:01:15

标签: android android-softkeyboard barcode-scanner textinput

您好,这是我的第一个问题 情况就是这样:我正在制作一个带有特定按钮来扫描条形码的服装软键盘,它打开条形码扫描器,扫描后,它返回上一个屏幕(比如消息应用程序),结果是剪辑或共享偏好。所有精细和花花公子,除了EditText中的结果来自旧扫描而不是新扫描的事实,因为当第一个活动获得结果时此时扫描没有完成(在启动条形码扫描仪活动之前)我是尝试使用布尔测试在on键函数中使用handler.postDelayed等待扫描完成但是这会在启动扫描之前冻结活动,因为我没有响应错误 关键功能是:

 case 2017:
            //Toast.makeText(FloatingViewService.this, "NormalScan.", Toast.LENGTH_LONG).show();
            test=true;
            InputConnection ic  = getCurrentInputConnection();
            ic.deleteSurroundingText(100, 100);  //pour vider le champs de saisair
            Intent dialogIntent = new Intent(SimpleIME.this, NormalScan.class);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity( dialogIntent);
            ic.commitText(String.valueOf(pref.getString("code", null)),1);

用于扫描条形码的NormalScan.class是:

public class NormalScan  extends AppCompatActivity {

    private ClipboardManager myClipboard;
    private ClipData myClip;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        IntentIntegrator integrator = new IntentIntegrator(this);
       // integrator.setOrientationLocked(false);
        integrator.setPrompt("Scan a barcode or QRcode");
        integrator.initiateScan();


        // integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
//        Use this for more customization
//integrator.setOrientationLocked(true);
//        IntentIntegrator integrator = new IntentIntegrator(this);

//        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);

//        integrator.setPrompt("Scan a barcode");

//        integrator.setCameraId(0);  // Use a specific camera of the device

//        integrator.setBeepEnabled(false);

//        integrator.setBarcodeImageEnabled(true);

//        integrator.initiateScan();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        Iconify
                .with(new FontAwesomeModule())
                .with(new EntypoModule())
                .with(new TypiconsModule())
                .with(new MaterialModule())
                .with(new MaterialCommunityModule())
                .with(new MeteoconsModule())
                .with(new WeathericonsModule())
                .with(new SimpleLineIconsModule())
                .with(new IoniconsModule());//// toast library

        SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor editor = prefs.edit();

        if (result != null) {
            if (result.getContents() == null) {
                // Toast.makeText(this, "Opération annulée", Toast.LENGTH_LONG).show();
                //PrettyToast.showError(getApplicationContext(), "Opération annulée");
                new PrettyToast.Builder(getApplicationContext())
                        .withMessage("    Opération annulée  ✘  ") // ☑ ☒ ☓ ✓ ✓ ✕ ✖ ✗ ✘
                        .withDuration(Toast.LENGTH_SHORT)
                        .withGravity(new PrettyToast.Gravity(Gravity.BOTTOM, 15, 0))
                        .withTextSize(24)
                        .withBackgroundResource(R.drawable.background_toast_red)
                        .withTextColor(R.color.white)
                        .build()
                        .show();
                //myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
                //String a = new String();
                //a=result.getContents();
                // myClip = ClipData.newPlainText("text","Scan Echouer");
                // myClipboard.setPrimaryClip(myClip);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //Do something after 100ms

                        handler.postDelayed(this, 2000);
                    }
                }, 1500);
                finish();
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                //editor.putString("message", "annulée");
                //editor.putString("code", a);
                //editor.commit();
            } else {
                myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                String a = new String();
                a = result.getContents();
                myClip = ClipData.newPlainText("text", a);
                myClipboard.setPrimaryClip(myClip);
                //PrettyToast.showSuccess(getApplicationContext(), "Le codebar sauvegarder dans la presse papier","","");
                new PrettyToast.Builder(getApplicationContext())
                        .withMessage("    Scanned   ✓  ") //☑  ✓
                        .withDuration(Toast.LENGTH_SHORT)
                        .withGravity(new PrettyToast.Gravity(Gravity.BOTTOM, 15, 0))
                        .withTextSize(24)
                        .withBackgroundResource(R.drawable.background_toast_green)
                        .withTextColor(R.color.white)
                        .build()
                        .show();
                //Toast.makeText(getApplicationContext(), "Le codebar sauvegarder dans la presse papier",Toast.LENGTH_SHORT).show();


                finish();
                editor.putString("message", "codebarre");
                editor.putString("code", a);
                editor.putBoolean("testscan", true);
                editor.commit();
                // show keybord

                  // InputMethodManager imm = (InputMethodManager) getSystemService(SimpleIME.INPUT_METHOD_SERVICE);
                  // imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);


            }
        }
        else {
            // This is important, otherwise the result will not be passed to the fragment
            super.onActivityResult(requestCode, resultCode, data);

            finish();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

使用startActivityForResult启动活动。调用onActivityResult时,保存扫描的条形码。下次建立inputConnection时,插入条形码 - 这应该是EditText的新连接