如何在片段

时间:2017-06-03 05:47:13

标签: android fragment setcontentview

我知道之前已经提出了类似的问题,但我已经搜索过,无法找到解决方案。

我正在尝试按照here

所述设置条形码扫描器
public class MainFragment extends Fragment implements View.OnClickListener{

    private ZXingScannerView scannerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_warrant, container, false);

        ImageButton scanBar = (ImageButton) view.findViewById(R.id.scan_barcode);
        scanBar.setOnClickListener(this);

        Button checkImei = (Button) view.findViewById(R.id.btn_check);
        checkImei.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scan_barcode:
                scannerView = new ZXingScannerView(getActivity());
                scannerView.setResultHandler(new ZXingScannerResultHandler());
//                setContentView(scannerView);
                scannerView.startCamera();
                break;
            case R.id.btn_check:
                Toast.makeText(getActivity(), "Button", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler {

        @Override
        public void handleResult(Result result) {
            String resultCode = result.getText();
            Toast.makeText(getActivity(), resultCode, Toast.LENGTH_LONG).show();
//            setContentView(R.layout.activity_start);
            scannerView.stopCamera();
        }
    }
}

在Activity中工作但在片段中移动我使用setContentView会出错。是否有替代setContentView或可能是使用片段的不同库?

1 个答案:

答案 0 :(得分:1)

这是,。的链接 Official documentation

关于如何将ZXingScanner实施为Fragment

干杯!