如何在自定义对话框中显示二维码扫描结果?

How to show QR Code Scan result in a Custom Dialog?

我正在使用 ZXing compile 'me.dm7.barcodescanner:zxing:1.8.4' 库扫描二维码。我在自定义对话框布局中显示扫描结果时遇到问题。

有什么方法可以将扫描结果显示为 string in textView in custom dialog layout 吗?

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private ZXingScannerView mScannerView;


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

        mScannerView = new ZXingScannerView(this);
        setContentView(mScannerView);

        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.resumeCameraPreview(MainActivity.this);
    }

    @Override
    public void handleResult(Result rawResult) {

        Log.e("handler", rawResult.getText()); // Prints scan results
        Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)


        final String shareResult = rawResult.getText();`

我不想用这个方法。但效果很好

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setMessage(rawResult.getText());
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                //user pressed OK
                mScannerView.resumeCameraPreview(MainActivity.this);
            }
        });
        builder.setNeutralButton("Share", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, shareResult);
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        });
        AlertDialog alert1 = builder.create();
        alert1.show();

    }

我想用这个方法。但是没用。

  private void dialogHandler(String result) {

        final String shareResult = result;

        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setContentView(R.layout.customdialog);

        final ImageView click_ok = (ImageView) dialog.findViewById(R.id.click_ok);
        final ImageView click_share = (ImageView) dialog.findViewById(R.id.click_share);

        final TextView qrResult = (TextView) findViewById(R.id.qrResult);
        qrResult.setText(shareResult);

        click_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                mScannerView.resumeCameraPreview(MainActivity.this);
                dialog.dismiss();
            }
        });

        click_share.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, shareResult);
                sendIntent.setType("text/plain");
                getBaseContext().startActivity(sendIntent);

            }
        });

        dialog.show();
    }
}

看看这是否有效。

      @Override
      public void handleResult(Result rawResult) {

              Log.e("handler", rawResult.getText()); // Prints scan results
              Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)


              final String shareResult = rawResult.getText();

              final Dialog dialog = new Dialog(MainActivity.this);
              dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
              dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
              dialog.setContentView(R.layout.customdialog);

              final ImageView click_ok = (ImageView) dialog.findViewById(R.id.click_ok);
              final ImageView click_share = (ImageView) dialog.findViewById(R.id.click_share);

              final TextView qrResult = (TextView) findViewById(R.id.qrResult);
              qrResult.setText(shareResult);

              click_ok.setOnClickListener(new View.OnClickListener() {

                      @Override
                      public void onClick(View v) {
                              // TODO Auto-generated method stub

                              mScannerView.resumeCameraPreview(MainActivity.this);
                              dialog.dismiss();
                      }
              });

              click_share.setOnClickListener(new View.OnClickListener() {

                       @Override
                       public void onClick(View v) {
                               // TODO Auto-generated method stub
                               Intent sendIntent = new Intent();
                               sendIntent.setAction(Intent.ACTION_SEND);
                               sendIntent.putExtra(Intent.EXTRA_TEXT, shareResult);
                               sendIntent.setType("text/plain");
                               getBaseContext().startActivity(sendIntent);

                       }
               });

              dialog.show();

      }
final TextView qrResult = (TextView) dialog.findViewById(R.id.qrResult);
qrResult.setText(shareResult);