如何使扫描二维码后出现的纯文本可点击并将用户重定向到浏览器?
How to make plain text which appears after scanning a QR clickable and redirects the user to the browser?
我写了一段扫描二维码的代码。但是在扫描后它以纯文本显示数据,即使它是某些网站的 link 或 URL。我希望它在直接在浏览器中扫描后将用户重定向到特定网站。
public void handleResult(Result rawResult) {
// Do something with the result here
// Log.v("tag", rawResult.getText()); // Prints scan results
// Log.v("tag", rawResult.getBarcodeFormat().toString());
// Prints the scan format (qrcode, pdf417 etc.)
MainActivity.tvresult.setText(rawResult.getText());
onBackPressed();
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
在 .xml 文件中创建一个显示文本的 TextView 可点击:
<Textview
android:autoLink="web"
android:linksClickable="true"/>
将此属性添加到您的 TextView
<Textview
android:autoLink="web"/>
其他选项是在用户点击您的 TextView
时为其创建一个 Intent
Uri uri = Uri.parse(rawResult.getText());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
更好的方法是创建一个 Linkify
Linkify.addLinks(tvresult, Linkify.WEB_URLS);
你可以选择你想要的。
我写了一段扫描二维码的代码。但是在扫描后它以纯文本显示数据,即使它是某些网站的 link 或 URL。我希望它在直接在浏览器中扫描后将用户重定向到特定网站。
public void handleResult(Result rawResult) {
// Do something with the result here
// Log.v("tag", rawResult.getText()); // Prints scan results
// Log.v("tag", rawResult.getBarcodeFormat().toString());
// Prints the scan format (qrcode, pdf417 etc.)
MainActivity.tvresult.setText(rawResult.getText());
onBackPressed();
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
在 .xml 文件中创建一个显示文本的 TextView 可点击:
<Textview
android:autoLink="web"
android:linksClickable="true"/>
将此属性添加到您的 TextView
<Textview
android:autoLink="web"/>
其他选项是在用户点击您的 TextView
Intent
Uri uri = Uri.parse(rawResult.getText());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
更好的方法是创建一个 Linkify
Linkify.addLinks(tvresult, Linkify.WEB_URLS);
你可以选择你想要的。