2017-07-16 62 views
-1

我是Android本地开发新手, 我想在警报消息中显示“在浏览器中打开”图标。安卓显示在浏览器中打开

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 

回答

-1

:如果你想与文本的按钮

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

+0

2问题无法解析openInBrowser();当我点击否,应用程序被绞死,这是关于警报,但我甚至想打开我得到一个警报的rawResult获取在另一个应用中打开或浏览器 –

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)); 
    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(); 
+0

感谢布鲁诺,这帮助我的警报,但这里是我的情况当我点击打开在浏览器中我需要搜索的结果我收到了Alert。案例:我从QR码中得到一个URL,我收到了一个警报(现在它已经在浏览器中打开:“,但是当我点击”在浏览器中打开“)需要打开我的警报(rarResult)中的URL –

+0

我用你需要的完整代码更新了这个问题 –

+0

Bruno。谢谢你的帮助。它工作正常。我在一段时间内与包管理员一起工作,希望你能帮助我。 –