2011-01-29 67 views
3

所以我有一個WebView內的LinearLayout與一個EditText框和一個按鈕,使應用程序搜索網絡。當我點擊我的按鈕時,它會將我帶到股票的Android瀏覽器。如何使它在我點擊去時保持在我的WebView中? 這裏是我的佈局:WebView裏面的應用程序?

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
     <EditText 
     android:id="@+id/web_address" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="http://"/> 

     <Button 
     android:id="@+id/go" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/go" 
     android:gravity="right"/> 

<WebView 
    android:id="@+id/webview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/> 
</LinearLayout> 

這裏是我的源:

package straightapp.com.Browser; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.view.KeyEvent; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.view.View.OnKeyListener; 
    import android.webkit.WebView; 
    import android.widget.Button; 
    import android.widget.EditText; 

    public class Browser extends Activity { 
WebView mWebView; 
Button button; 
EditText text; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mWebView = (WebView) findViewById(R.id.webview); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.loadUrl("http://www.straightapp.com"); 

     text=(EditText)findViewById(R.id.web_address); 
     button=(Button) findViewById(R.id.go); 
     button.setOnClickListener(new OnClickListener(){ 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    openBrowser(); 

    } 
     }); 
     text.setOnKeyListener(new OnKeyListener() { 

      public boolean onKey(View v, int keyCode, KeyEvent event) { 
        // TODO Auto-generated method stub 
        if (keyCode==KeyEvent.KEYCODE_ENTER){ 
          openBrowser(); 
          return true; 
        } 

        return false; 
      } 
      }); 
    } 

    public void openBrowser(){ 
     Uri uri=Uri.parse(text.getText().toString()); 
     Intent intent=new Intent(Intent.ACTION_VIEW,uri); 
     startActivity(intent); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
      mWebView.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
    } 

感謝

回答

1

準確地說,你可以嘗試以下變化:

public void openBrowser(){ 
    //Ensure that URL entered is a valid one 
    mWebView.loadUrl(text.getText().toString()); 
} 
2

當我點擊我的按鈕去需要我的股票的Android瀏覽器。

這是因爲這是你告訴Android要做的。 startActivity()開始活動。

我該如何讓它保持在我的WebView中,當我點擊去?

撥打loadUrl()撥打WebView而不是撥打startActivity()

+0

so insted:public void openBrowser(){ Uri uri = Uri.parse(text.getText()。toString()); Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); – Christian 2011-01-29 20:46:53

+0

我會有:公共無效openBrowser(){ Uri uri = Uri.parse(text.getText()。toString()); Intent intent = new Intent(Intent.ACTION_VIEW,uri); loadUrl(intent); – Christian 2011-01-29 20:47:30

0

更改XML如下文件:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".Browser"> <!--Just add this line --> 
     <EditText 
     android:id="@+id/web_address" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="http://"/> 

     <Button 
     android:id="@+id/go" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/go" 
     android:gravity="right"/> 

<WebView 
    android:id="@+id/webview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/> 
</LinearLayout> 
0

首先聲明的網頁流量和進度

progressBar = (ProgressBar)findViewById(R.id.progressBar); 
    webView = (WebView) findViewById(R.id.webView); 
    webView.setWebViewClient(new myWebClient()); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://www.google.com"); 

複製粘貼下面的代碼在創建方法

public class myWebClient extends WebViewClient { 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     super.onPageStarted(view, url, favicon); 
    } 

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

     progressBar.setVisibility(View.VISIBLE); 
     view.loadUrl(url); 
     return true; 

    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url); 

     progressBar.setVisibility(View.GONE); 
    } 
相關問題