2012-08-03 32 views
0

我有兩個活動「主」(帶有一個按鈕)&「第二」,我想當我點擊主活動中的按鈕時,第二活動應該是推出,但它應該像瀏覽器一樣。意味着瀏覽器應該在第二項活動啓動時打開。在Android中如何使我的活動像瀏覽器一樣行爲

我的代碼如下:
清單文件,我已經寫了

<activity android:name=".Second"> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.BROWSABLE"/> 
     </intent-filter> 
    </activity> 

在Main.java文件我已經寫了事件處理這樣

Button b = (Button) findViewById(R.id.button1); 

b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     // TODO Auto-generated method stub 

     Intent intent = new Intent(Main.this, Second.class); 
     startActivity(intent); 
    } 
}); 

首先,是這可能是我想要的,如果是的話,我會以正確的方式做到嗎?真的很抱歉,如果我的問題是愚蠢的,因爲我是Android的新手

+0

你解決了你的問題嗎? – MAC 2012-08-03 09:25:19

回答

1

當我開始學習關於An​​droid和它的意圖,我發現它非常混亂。 但清單中的類別BROWSABLE執行以下操作。

瀏覽器可以安全地調用目標活動以顯示鏈接引用的數據 - 例如圖像或電子郵件。

瞭解更多關於:http://developer.android.com/guide/components/intents-filters.html

兩個其他答案打開標準網頁瀏覽器,並轉到指定的地址。如果你想定製的瀏覽器進行第二活性喜歡這樣例如,一個Web視圖:

http://www.mkyong.com/android/android-webview-example/

祝你好運!

1

在第二項活動中寫下。由於你的第二個活動將會調用它將打開一個瀏覽器。

你也可以在onClick()中使用它。

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))); 
0

好像你試圖打開瀏覽器到特定的頁面,當按鈕被點擊主要活性。

如果這是你並不需要第二個活動的情況下,您需要做的僅僅是這樣的:

Button b = (Button) findViewById(R.id.button1); 

b.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
     startActivity(i); 
    } 
} 
1
public class WebDialog extends Dialog 
{ 

    static final int      BLUE     = 0xFF6D84B4; 
    static final float[]     DIMENSIONS_DIFF_LANDSCAPE = 
                    { 20, 60 }; 
    static final float[]     DIMENSIONS_DIFF_PORTRAIT = 
                    { 40, 60 }; 
    static final FrameLayout.LayoutParams FILL     = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); 
    static final int      MARGIN     = 4; 
    static final int      PADDING     = 2; 
    static final String     DISPLAY_STRING   = "touch"; 

    private String      mUrl; 
// private DialogListener    mListener; 
    private ProgressDialog    mSpinner; 
    private WebView      mWebView; 
    private LinearLayout     mContent; 
    private TextView      mTitle; 

    public WebDialog(Context context, String url) 
    { 
     super(context); 
     mUrl = url; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     mSpinner = new ProgressDialog(getContext()); 
     mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     mSpinner.setMessage("Loading..."); 

     mContent = new LinearLayout(getContext()); 
     mContent.setOrientation(LinearLayout.VERTICAL); 
     setUpTitle(); 
     setUpWebView(); 
     Display display = getWindow().getWindowManager().getDefaultDisplay(); 
     final float scale = getContext().getResources().getDisplayMetrics().density; 
     int orientation = getContext().getResources().getConfiguration().orientation; 
     float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT; 
     addContentView(mContent, new LinearLayout.LayoutParams(display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), display.getHeight() - ((int) (dimensions[1] * scale + 0.5f)))); 
    } 

    private void setUpTitle() 
    { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     Drawable icon = getContext().getResources().getDrawable(R.drawable.ic_launcher); 
     mTitle = new TextView(getContext()); 
     mTitle.setText("Website"); 
     mTitle.setTextColor(Color.WHITE); 
     mTitle.setTypeface(Typeface.DEFAULT_BOLD); 
     mTitle.setBackgroundColor(BLUE); 
     mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN); 
//  mTitle.setCompoundDrawablePadding(MARGIN + PADDING); 
//  mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 
     mContent.addView(mTitle); 
    } 

    private void setUpWebView() 
    { 
     mWebView = new WebView(getContext()); 
     mWebView.setVerticalScrollBarEnabled(false); 
     mWebView.setHorizontalScrollBarEnabled(false); 
     mWebView.setWebViewClient(new WebDialog.DialogWebViewClient()); 
     mWebView.getSettings().setJavaScriptEnabled(true); 

     System.out.println(" mURL = "+mUrl); 

     mWebView.loadUrl(mUrl); 
     mWebView.setLayoutParams(FILL); 
     mContent.addView(mWebView); 
    } 

    private class DialogWebViewClient extends WebViewClient 
    { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      view.loadUrl(url); 

      return true; 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
     { 
      super.onReceivedError(view, errorCode, description, failingUrl); 
      WebDialog.this.dismiss(); 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) 
     { 
      super.onPageStarted(view, url, favicon); 
      mSpinner.show(); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) 
     { 
      super.onPageFinished(view, url); 
      String title = mWebView.getTitle(); 
      if (title != null && title.length() > 0) 
      { 
       mTitle.setText(title); 
      } 
      mSpinner.dismiss(); 
     } 

    } 
} 
相關問題