得到zxing数据进入刷卡视图片段 [英] get zxing data into swipe view fragment
问题描述
我正在使用viewpager标签的片段zxing。我可以打电话给zxing意图和读取QR code,但我不能让导致到的EditText。
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.
这是我创造FragmentPagerAdapter标签:
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();
}
}
和我的 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 starts; then reads; then ends; but with no scan data: I think 'onActivityResult' never starts :(
我如何可以使用zxing在这个片段?
How can i use zxing in this fragment?
推荐答案
IntentIntegrator
有另一个构造函数,取片段。相反的:
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();
}
});
您可以使用:
scanBarcode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(Tab1.this);
scanIntegrator.initiateScan();
}
});
和它应该调用的onActivityResult
的片段直接。
and it should invoke onActivityResult
for the Fragment directly.
表示从一个活动,并从一个片段开始的意图,在安装有杆code扫描器二者的工作使用一个例子,是体现在这个GitHub库。
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.
如果您使用的是V4支持库碎片,<一个href=\"https://$c$c.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegratorSupportV4.java?r=2122\"相对=nofollow> IntentIntegratorSupportV4
类提供兼容性:
If you are using the v4 support library Fragments, the IntentIntegratorSupportV4
class provides compatibility:
IntentIntegrator scanIntegrator = new IntentIntegratorSupportV4(Tab1.this);
这篇关于得到zxing数据进入刷卡视图片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!