2017-07-16 35 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。謝謝你的幫助。它工作正常。我在一段時間內與包管理員一起工作,希望你能幫助我。 –