ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안드로이드 webview 웹뷰 사용법
    program_language 및 궁금한것/Android Studio 2018. 7. 29. 17:46
    <uses-permission android:name="android.permission.INTERNET"/>

    이걸 Mainfast에 써줘야지 인터넷 웹뷰가 사용가능하다.

    이 권한을 부여안해준다면 err_cache_miss란 에러가 뜬다 꼭. 꼭. 추가하도록

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    tools:context=".MainActivity">
    <RelativeLayout
    android:id="@+id/top_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
    android:id="@+id/et_url"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/btn_move"
    android:text="http://google.co.kr" />
    <Button
    android:id="@+id/btn_move"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="이동" />
    </RelativeLayout>
    <RelativeLayout
    android:id="@+id/middle_layout"
    android:layout_above="@id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/top_layout">
    <WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    </RelativeLayout>
    <RelativeLayout
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    >
    <Button
    android:id="@+id/btn_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back" />
    <Button
    android:id="@+id/btn_forward"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="forward"
    android:layout_toRightOf="@id/btn_back"
    />
    </RelativeLayout>
    </RelativeLayout>
    package com.example.webview;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Button;
    import android.widget.EditText;
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_url;
    private Button btn_move, btn_back, btn_forward;
    private WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_url = findViewById(R.id.et_url);
    btn_move = findViewById(R.id.btn_move);
    btn_back = findViewById(R.id.btn_back);
    btn_forward = findViewById(R.id.btn_forward);
    webview = findViewById(R.id.webview);
    // 이벤트 장착
    webview.setWebViewClient(new MyBrowser());
    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setBuiltInZoomControls(true);
    btn_move.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String url = et_url.getText().toString();
    if (url != null && url.length() > 0) {
    webview.loadUrl(url);
    }
    }
    });
    }// oncreate close
    private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
    }
    }
    @Override
    public void onClick(View v) {
    }
    }


    모두의 게시판 이걸로 만들면되! 바부야.


    반응형

    댓글

Designed by Tistory.