我是Android Native Development的新手, 我想在警告消息中显示“在浏览器中打开”图标。
Case:
I have a QR code scanned and showing the result in a toast or Alert.
In the scanned Result, I want to Show up "Open In Browser" with the code result or Alert message
挑战是我无法实现此功能。请让我知道如何做到这一点,以便我可以进一步学习Android。
My Manifest goes like this:
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And my Result handler
@Override
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.)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("UTQode Result");
builder.setMessage(rawResult.getText());
AlertDialog alert1 = builder.create();
alert1.show();
// Toast.makeText(getApplicationContext(), "My Message",
// Toast.LENGTH_SHORT).show();
Toast.makeText(this, rawResult.getText(), Toast.LENGTH_LONG).show();
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
I am testing with the above code, but I need to open the alert result and Query it in Browser
答案 0 :(得分:0)
在警告对话框中执行此操作:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("UTQode Result");
builder.setMessage(rawResult.getText());
builder..setIcon(getResources().getDrawable(android.R.drawable.your_icon));
AlertDialog alert1 = builder.create();
alert1.show();
如果您想要一个“在浏览器中打开”文本的按钮,请执行以下操作:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("UTQode Result");
builder.setMessage(rawResult.getText());
builder.setIcon(getResources().getDrawable(android.R.drawable.your_icon));
builder.setPositiveButton("Open in browser", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(your result url)));
}
});
AlertDialog alert1 = builder.create();
alert1.show();
答案 1 :(得分:-1)
如果要通过单击“在浏览器中打开”打开浏览器,可以按下按钮添加方法
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Open in Browser",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
openInBrowser();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
如果您只想在警报顶部使用图标,请使用.setIcon()