【问题标题】:WebView inside application?应用程序内部的WebView?
【发布时间】:2011-01-29 20:15:07
【问题描述】:

所以我在 LinearLayout 中有一个 WebView,它带有一个 EditText 框和一个按钮,用于使应用程序搜索网络。当我点击我的按钮时,它会将我带到股票 android 浏览器。当我点击 go 时,如何制作它以便它留在我的 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);
    }
  }

谢谢

【问题讨论】:

    标签: android web-applications


    【解决方案1】:

    当我点击我的按钮时,它会将我带到股票 android 浏览器。

    那是因为那是您告诉 Android 要做的事情。 startActivity() 开始一个活动。

    如何制作它,以便在我单击 go 时它留在我的 WebView 中?

    在您的WebView 上调用loadUrl(),而不是调用startActivity()

    【讨论】:

    • 如此安装:public void openBrowser(){ Uri uri=Uri.parse(text.getText().toString()); Intent intent=new Intent(Intent.ACTION_VIEW,uri);开始活动(意图);
    • 我会:public void openBrowser(){ Uri uri=Uri.parse(text.getText().toString()); Intent intent=new Intent(Intent.ACTION_VIEW,uri); loadUrl(intent);
    • @Christian:不,这甚至不会编译。请参阅@TheCottonSilk 的回答。
    • 是的,我在尝试时发现了这一点。所以要确保这不是你的意思。
    【解决方案2】:

    确切地说,您可以尝试以下更改:

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

    【讨论】:

    • 这只是把我带到了股票浏览器。
    • @Christian:可能是您尝试加载的网页执行了重定向。重定向和点击WebView 中的链接,默认情况下会在用户的默认浏览器中打开。您可以使用WebViewClientshouldOverrideUrlLoading() 来更改此行为。
    • @CommnsWare:对不起,我有点菜鸟。我会放这些吗?
    • @TheCottonSilk:当我添加 setContentView(mWebView);当我单击“开始”按钮时,程序刚刚关闭。我确实添加了 mWebView=(WebView) findViewById(R.id.webview);但它仍然强制关闭。
    • 对不起,我的错误。我没有在 onCreate() 中看到 setContentView() 调用。您尝试加载哪个测试 URL?
    【解决方案3】:

    如下修改你的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>
    

    【讨论】:

      【解决方案4】:

      首先声明webview和progressbar

          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");
      

      将此代码复制并粘贴到 onCreate 方法下方

         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);
          }
      

      【讨论】:

        猜你喜欢
        • 2018-01-25
        • 1970-01-01
        • 2013-04-28
        • 2021-06-25
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 2014-05-17
        • 2023-03-31
        相关资源
        最近更新 更多