【发布时间】:2017-01-05 05:10:53
【问题描述】:
我目前正在开发一款带有条形码扫描仪的应用。目前,当我扫描条形码时,它也应该显示条形码的格式和内容。但目前扫描后不显示格式和内容。
这里是代码
public class ThreeFragment extends Fragment{
public ThreeFragment() {
// Required empty public constructor
}
Button scan_btn;
EditText Edit_current;
TextView formatTxt, contentTxt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
scan_btn = (Button) view.findViewById(R.id.scan_button);
final Activity activity = getActivity();
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result.getContents() != null) {
if (result.getContents() != null){
String scanContent = result.getContents();
String scanFormat = result.getFormatName();
// display it on screen
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
} else {
Toast toast = Toast.makeText(getContext(),"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
我的 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button android:id="@+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/scan" />
<TextView
android:id="@+id/scanFormat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/scan_button" />
<TextView
android:id="@+id/scanContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/scanFormat" />
</RelativeLayout>
我已经将代码编辑到这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
scan_btn = (Button) view.findViewById(R.id.scan_button);
formatTxt = (TextView) view.findViewById(R.id.scanFormat);
contentTxt = (TextView) view.findViewById(R.id.scanContent);
final Activity activity = getActivity();
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result.getContents() != null) {
if (result.getContents() == null){
Toast toast = Toast.makeText(getContext(),"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}else{
String scanContent = result.getContents();
String scanFormat = result.getFormatName();
// display it on screen
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
}
}
}}
还是不能显示格式和内容
【问题讨论】:
-
你在哪里初始化了 formatTxt ?
-
如果您可能删除了外部多余的
if (result.getContents() != null) {语句,您将能够看到失败的 toast。 -
我已将代码更改为
-
@jason 我已经更新了我的答案,看看那里!在我的设备上测试。它的工作
-
我使用了您的代码,但仍然没有显示。已经调试了这些行并且它们没有被调用
标签: java android barcode zxing barcode-scanner