将zxing数据导入滑动视图片段

时间:2014-12-28 21:42:06

标签: android android-fragments zxing

我在viewpager选项卡的片段中使用zxing。我可以用Intent调用zxing并读取QR码,但我无法将结果发送到edittext。

这是我在FragmentPagerAdapter中创建标签的地方:

@Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            return new Tab1();
        case 1:
            return new Tab2();
        default:
            return new EmptyTab();
        }
    }

我的 Tab1 类:

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开始;然后读;然后结束;但没有扫描数据:我认为' onActivityResult'从不开始:(

我如何在这个片段中使用zxing?

3 个答案:

答案 0 :(得分:4)

IntentIntegrator有另一个构造函数,使用Fragment。而不是:

scanBarcode.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
        scanIntegrator.initiateScan();
    }
});

你可以使用:

scanBarcode.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        IntentIntegrator scanIntegrator = new IntentIntegrator(Tab1.this);
        scanIntegrator.initiateScan();
    }
});

它应该直接为片段调用onActivityResult

示例显示安装了条形码扫描程序is demonstrated in this GitHub repo时,从Activity和Fragment启动intent的工作用法。


如果您使用的是v4支持库片段,则IntentIntegratorSupportV4类提供兼容性:

IntentIntegrator scanIntegrator = new IntentIntegratorSupportV4(Tab1.this);

答案 1 :(得分:2)

将此添加到您的清单

    <!-- 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>

然后将onclick方法更改为此

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

最后将结果更改为此

if(requestCode==0)
    if(resultCode==Activity.RESULT_OK){
         Toast.makeText(getActivity(),"Scan Result = " + data.getStringExtra("SCAN_RESULT"),
             Toast.LENGTH_SHORT).show();
    }

答案 2 :(得分:2)

对于较新的Zxing BarcodeScanner库。 IntentIntegratorSupportV4(Fragment.this)不可用。你可以用它。

IntentIntegrator integrator = IntentIntegrator.forSupportFragment(Fragment.this);
 integrator.initiateScan();