如何使用editText为URL和按钮单击打开另一个活动中的webview

时间:2016-10-27 18:35:23

标签: java android android-layout webview

MainActivity.java

public class MainActivity extends Activity {

    WebView web1;
    EditText ed1;
    Button bt1;
    String Address;
    ProgressBar pbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final Context context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web1 = (WebView) findViewById(R.id.webView1);
        ed1 = (EditText) findViewById(R.id.editText1);
        bt1 = (Button) findViewById(R.id.button1);

        pbar.setVisibility(View.GONE);

        bt1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, WebViewActivity.class);
                startActivity(intent);


                Address = "http://" + ed1.getText().toString();
                WebSettings webSetting = web1.getSettings();
                webSetting.setBuiltInZoomControls(true);
                webSetting.setJavaScriptEnabled(true);
                setContentView(R.layout.activity_main);

//下面给出了这个错误,说明新的WebViewClient无法解析。

                web1.setWebViewClient(new WebViewClient());

                web1.loadUrl(Address);

            }
        });
    }
}

WebViewActivity.java

public class WebViewActivity extends Activity {


    WebView web1;

    String Address;
    String add;
    ProgressBar pbar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        web1 = (WebView)findViewById(R.id.webView1);

        pbar = (ProgressBar)findViewById(R.id.progressBar1);
        pbar.setVisibility(View.GONE);

        web1.setWebViewClient(new WebViewActivity.WebViewClient());

    }


    public class WebViewClient extends android.webkit.WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            pbar.setVisibility(View.VISIBLE);
        }

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

            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {

            // TODO Auto-generated method stub

            super.onPageFinished(view, url);
            pbar.setVisibility(View.GONE);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
            web1.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

1 个答案:

答案 0 :(得分:1)

使用Intent将url作为字符串传递给webview活动:

Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("URL",url)
startActivity(intent);

通过以下方式在WebViewActivity onCreate中访问它:

Intent in = getIntent();
String url = in.getStringExtra("URL");

现在在

中传递此网址
weview.loadurl(url);

<强> MainActivity

public class MainActivity extends AppCompatActivity {

Button b1;
EditText ed1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1 = (Button) findViewById(R.id.button);
    ed1 = (EditText) findViewById(R.id.editText);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = ed1.getText().toString();
            Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
            intent.putExtra("URL", url);
            startActivityForResult(intent, 1);

        }
    });
}


}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GO"/>

  </LinearLayout>

<强> WebViewActivity

public class WebViewActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    String url = getIntent().getStringExtra("URL");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl(url);
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

<强> webview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</WebView>

</LinearLayout>

<强> AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mywebviewapplication">

<uses-permission android:name="android.permission.INTERNET" />

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

    <activity android:name=".WebViewActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
</application>

</manifest>