【问题标题】:QR code comparison to string ANDROIDQR 码与字符串 ANDROID 的比较
【发布时间】:2017-03-02 17:38:53
【问题描述】:

我正在使用 compile 'com.google.android.gms:play-services:10.2.0' (https://developers.google.com/android/reference/com/google/android/gms/vision/barcode/Barcode.html#valueFormat) 来读取二维码。我想将我读取的 QR 与字符串进行比较。如果匹配,它应该显示一条消息。

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        if (data != null) {
            final Barcode barcode = data.getParcelableExtra("barcode");
            // I also tried barcode.displayValue!
            final String password = barcode.rawValue;

            if (password == "123456") {
                resultText.post(new Runnable() {
                    @Override
                    public void run() {
                        resultText.setText("Sucess");
                    }
                });

当我显示我的二维码时,它显示为 123456,但无法将其与“123456”进行比较。

我认为 displayValue 和 rawValue 将我的 QR 转换为字符串。有人有什么主意吗? 谢谢

【问题讨论】:

    标签: android qr-code


    【解决方案1】:

    您必须使用password.equals("123456") 来比较您的字符串。 == 运算符检查字符串是否是完全相同的对象,而不是具有相同的值。

      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                final Barcode barcode = data.getParcelableExtra("barcode");
                // I also tried barcode.displayValue!
                final String password = barcode.rawValue;
    
                if (password.equals("123456")) {
                    resultText.post(new Runnable() {
                        @Override
                        public void run() {
                            resultText.setText("Sucess");
                        }
                    });
    

    【讨论】:

    • 感谢您的评论。这很简单,但解释得很好。 =] 它解决了我的问题。
    【解决方案2】:

    使用 equals 如果它们相等则返回 true,否则为 details 返回 false

    password.equals("12345")
    

    【讨论】:

      【解决方案3】:

      使用password.equals("123456"),对比较字符串有效。我希望这能解决您的问题。 as "==" 比较字符串中值的引用,而 as .equals() 比较实际值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 2011-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        相关资源
        最近更新 更多