开发者

WebView in android [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_运维问答 Closed 10 years ago.

How to set WebView in android?


HelloWorld example of WebView

Read more: WebView on Android developers


 **main.xml (res/layout)**
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
   android:orientation="vertical" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/urlEdit"
        android:layout_width="300dip"
        android:layout_height="wrap_content"
        android:inputType="textWebEditText"
        android:imeOptions="actionDone" />

    <Button
        android:id="@+id/goButton"
        android:layout_width="50dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_toRightOf="@id/urlEdit"
        android:background="@drawable/redbutton"
        android:text="Go..."
        android:textColor="#fcfcfc" />
  </RelativeLayout>

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

 </LinearLayout>

WebView Activtiy

   public class WebViewExampleActivity extends Activity  {
   public WebView webView;
   public Button goButton;
   public EditText urlEdit;
   @Override
   public void onCreate(Bundle savedInstanceState)   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webView = (WebView)findViewById(R.id.webView);
    goButton = (Button)findViewById(R.id.goButton);
    urlEdit = (EditText)findViewById(R.id.urlEdit); 

    webView.setWebViewClient(new DemoWebViewClient());
    goButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            openUrl();
        }
    });
     }

     public void openUrl()    {
    String url;
    url = urlEdit.getText().toString();
    webView.loadUrl(url);
     }
     public class DemoWebViewClient extends WebViewClient     {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    }
    }


Here they are

xml

<WebView
  android:id="@+id/web_view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"      
  android:layout_weight="1.0" />

class

    WebView webview = (WebView) findViewById(R.id.web_view);
    webview.getSettings().setSupportZoom(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.loadUrl("file:///android_asset/yourHtmlFile.html");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜