Zxing Scanner App,如何将结果显示为是或否?

时间:2017-11-16 02:53:42

标签: java android zxing barcode-scanner

在Android Studio上使用zxing,空活动和4.4 KitKat。我想将结果显示为是或否。我想引用带有UPC代码的谷歌表。如果UPC已经在电子表格中,则显示“是”或者如果UPC不在电子表格中显示“否”。我可以仅使用google工作表上的UPC列在MainActivity java代码中使用“If,Else”吗?

@Override
        protected void onActivityResult ( int requestCode, int resultCode, Intent data){
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (result != null) {
                if (result.getContents() == null) {
                    Log.d("MainActivity", "Cancelled scan");
                    Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
                } else {
                    Log.d("MainActivity", "Scanned");
                    Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                }
            } else {
                super.onActivityResult(requestCode, resultCode, data);

我想根据扫描的条形码是否已经存在于电子表格中,将此代码显示为“是”或“否”。我的问题是如何让电子表格与扫描仪通信,或者可以用布尔值复制电子表格中的所有条形码?

Set<String> getStringSet (String key, 
            Set<String> defValues)

makeText(Context context, int resId, int duration)

protected void onActivityResult ( int requestCode, int resultCode, Intent data){
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (result != null) {
                if (result.getContents() == null) {
                    Log.d("MainActivity", "Cancelled scan");
                    Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
                } else {
                    Log.d("MainActivity", "Scanned");
                    Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                }

以上是我从搜索中得出的结果。使用共享首选项来存储我的数据(1000 UPC)使用ZXING进行解析以及IF ELSE检查代码是否切实可行,最后TOAST显示“是”或“否”基于我的结果是否位于共享中偏好?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = this.findViewById(R.id.button);
    final Activity activity = this;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scan");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            Log.d("MainActivity", "Cancelled scan");
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Log.d("MainActivity", "Scanned");
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

public void scanCode(View view) {
}

public class myWebView extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        WebView webview = new WebView(this);
        setContentView(webview);



        WebView myWebView = findViewById(R.id.myWebView);



        myWebView.setWebViewClient(new MyWebViewClient());

        WebSettings webSettings = myWebView.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webview.loadUrl("https://google.com/");
    }


    private class MyWebViewClient extends WebViewClient {

        @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {


            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

            startActivity(intent);

            return true;
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

用于隔离代码,您需要创建一个单独的活动,将SacnActivity作为一个单独的活动,扩展活动并触发ZXingScannerView.ResultHandler并从MainActivity调用并在onActivityResult()中回调Mainactivity。

MainActivity

public class MainActivity extends Activity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scan = (Button)findViewById(R.id.btnScan);


        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivityForResult(new Intent(MainActivity.this,SacnActivity .class),1);

            }
        });


    }

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                String result=data.getStringExtra("result");
                Log.d("result================>",""+result);

                try {
                    //processData(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (resultCode == Activity.RESULT_CANCELED) {