Oto mój kod:
package com.test.browsertest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class BrowserTest extends Activity{
/** Called when the activity is first created. */
private WebView browser;
private EditText adressline;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Finding View for buttons etc.
adressline = (EditText)findViewById(R.id.adress);
Button go = (Button)findViewById(R.id.enter);
browser = (WebView)findViewById(R.id.browser);
Button stop = (Button)findViewById(R.id.stop);
Button clear = (Button)findViewById(R.id.clear);
//Action for clicking button go
go.setOnClickListener(new OnClickListener() {
public void onClick(View v){
openBrowser();
}
private void openBrowser() {
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl(adressline.getText().toString());
}
});
//Action for clicking button stop
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v){
browser.stopLoading();
}
});
clear.setOnClickListener(new OnClickListener() {
public void onClick(View v){
adressline.setText("http://");
}
});
}
}
Powinno wyświetlać stronę w WebView pod przyciskami, a mimo to i tak otwiera zewnętrzną przeglądarkę i tam otwiera stronę.
Ktoś wie dlaczego?
Tutaj jest plik main.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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/adress"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:singleLine="true"
android:text="http://"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/x"
/>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear"
/>
<Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter"
/>
</LinearLayout>
<WebView android:id="@+id/browser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
</LinearLayout>
Dodam, że w manifeście zadeklarowałem korzystanie z internetu.