问题
I am using zxing in a fragment of viewpager tab. I can call zxing with Intent and read the QR code but I can't get result to a edittext.
This is where i create tab in FragmentPagerAdapter:
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Tab1();
case 1:
return new Tab2();
default:
return new EmptyTab();
}
}
And my Tab1 class:
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_1, container, false);
IDEditText = (EditText) rootView.findViewById(R.id.fttx_id_editText);
Button scanBarcode = (Button) rootView.findViewById(R.id.scan_barcode);
scanBarcode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity);
scanIntegrator.initiateScan();
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanningResult != null) {
// we have a result
String scanContent = scanningResult.getContents();
IDEditText .setText("CONTENT: " + scanContent);
} else {
Toast.makeText(getActivity(),
"No scan data received!", Toast.LENGTH_SHORT).show();
}
}
zxing starts; then reads; then ends; but with no scan data: I think 'onActivityResult' never starts :(
How can i use zxing in this fragment?
回答1:
IntentIntegrator
has another constructor, taking the Fragment. Instead of:
scanBarcode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.initiateScan();
}
});
you can use:
scanBarcode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(Tab1.this);
scanIntegrator.initiateScan();
}
});
and it should invoke onActivityResult
for the Fragment directly.
An example showing working usage of both starting the intent from an Activity and from a Fragment, with the barcode scanner installed, is demonstrated in this GitHub repo.
If you are using the v4 support library Fragments, the IntentIntegratorSupportV4 class provides compatibility:
IntentIntegrator scanIntegrator = new IntentIntegratorSupportV4(Tab1.this);
回答2:
add this to your manifest
<!-- Zxing scan activity -->
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
and then change the onclick method to this
Intent intent = new Intent(context, CaptureActivity.class);
intent.setAction("com.google.zxing.client.android.SCAN");
// this stops saving ur barcode in barcode scanner app's
// history
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);
and finally change the on result to this
if(requestCode==0)
if(resultCode==Activity.RESULT_OK){
Toast.makeText(getActivity(),"Scan Result = " + data.getStringExtra("SCAN_RESULT"),
Toast.LENGTH_SHORT).show();
}
回答3:
For newer Zxing BarcodeScanner library. IntentIntegratorSupportV4(Fragment.this)
is not available. You can use this.
IntentIntegrator integrator = IntentIntegrator.forSupportFragment(Fragment.this);
integrator.initiateScan();
来源:https://stackoverflow.com/questions/27680863/get-zxing-data-into-swipe-view-fragment