반응형
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a49_web">
<!-- 인터넷 접속 권한 설정 -->
<uses-permission android:name="android.permission.INTERNET" />
<application
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/Theme.A49_Web"
android:usesCleartextTraffic="true">
<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:usesCleartextTraffic="true">
MainActivity
package com.example.a49_web;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceRequest;
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 {
EditText editText;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editTextTextPersonName);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// 자바스크립트가 동작할 수 있는 환경이 됨
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new ViewClient());
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl(editText.getText().toString());
}
});
}
private class ViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String request) {
view.loadUrl(request);
return true;
}
}
}
반응형