当我扫描qr时,结果不会显示在android studio的编辑文本中?

q5iwbnjs  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(326)

如果在android studio中有一个小应用程序,我扫描一个qrcode,结果应该显示在第一个edittext“number”中。但这不会发生。我用textview尝试了这个,但是文本也没有改变。当我按下“扫描”按钮,然后扫描,然后文本应该改变时,如何设置文本?

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class verleih extends AppCompatActivity {

EditText number;
EditText name;
EditText complete;
EditText where;

Button find;
Button add;
Button update;
Button delete;
Button scan;

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verleih);

    number=findViewById(R.id.editText_nummer);
    name=findViewById(R.id.editText_name);
    complete=findViewById(R.id.editText_vollstaendig);
    where=findViewById(R.id.editText_wo);

    find=findViewById(R.id.button_find);
    add=findViewById(R.id.button_add);
    update=findViewById(R.id.button_update);
    delete=findViewById(R.id.button_delete);
    scan=findViewById(R.id.button_scan);

    textView=findViewById(R.id.textView2);

    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            IntentIntegrator integrator = new IntentIntegrator(verleih.this);
            integrator.initiateScan();
        }
    });

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    String number_qr="";
    super.onActivityResult(requestCode, resultCode, intent);
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanResult!=null) {
        Toast.makeText(this,scanResult.getContents(),Toast.LENGTH_SHORT).show();
        number_qr=scanResult.getContents();

        number.setText(number_qr);
        textView.setText(number_qr);

    }else{
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    }

}

}

uqcuzwp8

uqcuzwp81#

在zxing文档中,他们提到如果用户取消扫描,那么 getContents() 将为空。如果为空,则不会在文本视图/编辑文本中显示任何内容。将空检查添加到 number_qr 确认你从图书馆得到数据。

相关问题