简体   繁体   中英

Getting a value and displaying it on an EditText

I have the following code, It's a barcode scanning App using the ZXing Library.

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 is the EditText where the the barcode result should be displayed. The result is got by the following code :

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

make txtoperatrice a global variable and then assign it via findViewById on your onCreate Method

and onActivityResult set its text to the result using the following code

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

If the object of EditText txtoperatrice is global in your class and give the id of XML component using findViewById(R.id.yourEditText) in onCreate function in the activity.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM