亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android Studio - 當“if”被滿足時如何觸發意圖

Android Studio - 當“if”被滿足時如何觸發意圖

弒天下 2022-11-10 09:42:09
我設法讓一個由谷歌視覺提供支持的二維碼掃描儀工作,并將二維碼放入同一活動的文本視圖中。最終目標是在檢測到 qrcode 后立即在另一個活動 (QRWebActivity) 的 web 視圖中打開 qrcode 中的 url。在這個階段,我能夠將 qrcode 移動到一個字符串中,然后使用 sendMessage2 在按鈕單擊時激活的意圖推動并在 web 視圖中打開。但我真的很想找到一種方法讓它自動打開 QRWebActivity 并將 webview 發送到 'if(qrCodes.size()!=0) 上的 qrCode。任何幫助都會很棒。如果我沒有使用正確的術語,真的很抱歉,我只是不知道我在做什么,但我真的很想在本周末完成這個應用程序以發布,我已經很接近了。        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {            @Override            public void release() {            }            @Override            public void receiveDetections(Detector.Detections<Barcode> detections) {                final SparseArray<Barcode> qrCodes = detections.getDetectedItems();                if(qrCodes.size()!=0)                {                    textView.post(new Runnable() {                        @Override                        public void run() {                            Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);                            vibrator.vibrate(1000);                            textView.setText(qrCodes.valueAt(0).displayValue);                        }                    });                }            }        });    }    public void sendMessage2 (View view)    {        String qrmessage = textView.getText().toString();        Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);        intent2.putExtra("EXTRA_QRMESSAGE",qrmessage);        startActivity(intent2);    }}如果我可以在 qrCodes !=0 時模擬按下按鈕并觸發“sendMessage2”,那我會這樣做......即使我確信有一種更優雅的方式。
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

從您的代碼中,這是我的解決方案:


@Override

public void receiveDetections(Detector.Detections<Barcode> detections) {

    final SparseArray<Barcode> qrCodes = detections.getDetectedItems();


    if (qrCodes.size() != 0) {

        textView.post(new Runnable() {

            @Override

            public void run() {

                Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

                vibrator.vibrate(1000);

                textView.setText(qrCodes.valueAt(0).displayValue);

                sendMessage2(textView);

            }

        });

    }

}


查看完整回答
反對 回復 2022-11-10
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

所有代碼都已經存在,只是不要拆分它:


        public void receiveDetections(Detector.Detections<Barcode> detections) {

            final SparseArray<Barcode> qrCodes = detections.getDetectedItems();


            if(qrCodes.size()!=0)

            {

                Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);

                intent2.putExtra("EXTRA_QRMESSAGE",qrCodes.valueAt(0).displayValue);

                startActivity(intent2);

                textView.post(new Runnable() {

                    @Override

                    public void run() {


                        Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

                        vibrator.vibrate(1000);

                        textView.setText(qrCodes.valueAt(0).displayValue);

                    }


                });

            }

        }


查看完整回答
反對 回復 2022-11-10
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號