[챕터 2-7-3] 웹 뷰

2020. 2. 18. 22:06Android/Android 챕터 2-7

반응형

결과화면

열기 버튼 클릭 시

 

 


프로젝트명 : MyWeb

 

web_view 에 id 부여 : web_view

 


MainActivity.java

더보기
package org.minokuma.myweb;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

        EditText editText = findViewById(R.id.editText);
        WebView webView = findViewById(R.id.web_view);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
    }
}


(*) API 레벨 15라서 21 요구

build.gradle 열어서 

minSdkVersion 15 를 minSdkVersion 21 로 수정한 후 싱크한다.

 


MainActivity.java

 

더보기
package org.minokuma.myweb;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

        final EditText editText = findViewById(R.id.editText);
        final WebView webView = findViewById(R.id.web_view);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl(editText.getText().toString());
            }
        });

    }
}

 


인터넷 권한 부여

 

AndroidManifest.xml

 

더보기
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.minokuma.myweb">

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

    <application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>
    </application>

</manifest>

 

반응형

'Android > Android 챕터 2-7' 카테고리의 다른 글

[챕터 2-7-5] 키패드  (0) 2020.02.18
[챕터 2-7-4] 시크바  (0) 2020.02.18
[챕터 2-7-2] 애니메이션 슬라이딩  (0) 2020.02.18
[챕터 2-7-1] 애니메이션  (0) 2020.02.18