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

时间:2016-08-22 09:08:51

标签: android dialog zxing

我正在使用ZXing compile 'me.dm7.barcodescanner:zxing:1.8.4'库来扫描QR码。我面临的问题是在自定义对话框布局中显示扫描结果。

有没有办法在string布局的textView中将扫描结果显示为custom dialog

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();
    }
}

2 个答案:

答案 0 :(得分:0)

看看是否有效。

      @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();

      }

答案 1 :(得分:0)

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