java获取值并将其显示在EditText上
我有以下代码,这是一个使用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
是应显示条形码结果的编辑文本。通过以下代码获得结果:
@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);
}
}
# 1 楼答案
如果EditText txtoperatrice的对象在您的类中是全局的,则在活动的onCreate函数中使用findViewById(R.id.yourEditText)给出XML组件的id
}
# 2 楼答案
将
txtoperatrice
设为全局变量,然后通过findViewById
在onCreate
方法上赋值和onActivityResult使用以下代码将其文本设置为结果