获取值并将其显示在EditText上

时间:2018-10-18 14:02:52

标签: java android barcode-scanner

我有以下代码,这是一个使用ZXing库的条形码扫描应用程序。

txtoperatrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scannez le code à barres SVP");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(true);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });
}

txtoperatrice是EditText,应在其中显示条形码结果。结果是通过以下代码获得的:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result!=null){
        if(result.getContents()==null){
            Toast.makeText(this, "Erreur de Scan", Toast.LENGTH_LONG).show();
        }
        else{
            ///////// DISPLAY THE CODE IN THE EDITTEXT txtoperatrice
        }
    }
    else{
        super.onActivityResult(requestCode, resultCode, data);
    }
}

2 个答案:

答案 0 :(得分:0)

txtoperatrice设置为全局变量,然后通过findViewById方法上的onCreate对其进行分配

和onActivityResult使用以下代码将其文本设置为结果

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result!=null){
        if(result.getContents()==null){
            Toast.makeText(this, "Erreur de Scan", Toast.LENGTH_LONG).show();
        }
        else{
           txtoperatrice.setText(result.getContents());
        }
    }
    else{
        super.onActivityResult(requestCode, resultCode, data);
    }
}

答案 1 :(得分:0)

如果EditText txtoperatrice对象在您的类中是全局对象,并在活动的onCreate函数中使用findViewById(R.id.yourEditText)给出XML组件的ID。

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result!=null){
        if(result.getContents()==null){
            Toast.makeText(this, "Erreur de Scan", Toast.LENGTH_LONG).show();
        }
        else{
            //you should have a string to put in this argument
            //get your result in a string for example resultStr
            String resultStr = result.getContents().tostring();
            //the above line is just to clear what to do. it may not work exacly 
            // and then...
            txtoperatrice.setText(resultStr);
        }
    }
    else{
        super.onActivityResult(requestCode, resultCode, data);
    }

}