我搜遍了各地,我找不到一個關於如何在片段中實現ZXing的工作解決方案。多個消息來源告訴我這是正確的,但onActivityResult永遠不會被調用。在片段中使用ZXing
點擊該按鈕觸發掃描儀打開
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_barcode,container,false);
final Button openBC = (Button)view.findViewById(R.id.btnOpen);
openBC.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//This opens up the Barcode Scanner
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan A barcode or QR Code");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
return view;
}
這就是掃描的結果應進行處理,但它從來沒有送過來
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(getActivity(), "canceled",Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(getActivity(),"Scanned: " + result.getContents(),Toast.LENGTH_LONG).show();
}
}else {
super.onActivityResult(requestCode, resultCode, data);
}
}
嘗試將片段傳遞給IntentIntegrator構造函數。另外你在哪裏重寫onActivityResult?在片段還是活動? – jgriffin
包含您的片段的活動的'onActivityResult'方法將被調用。 –
@jgriffin是的,我重寫了onActivityResult,並試圖傳入片段。 –