activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="URL을 입력하세요."
android:singleLine="true" />
<Button
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이동"
android:textSize="20dp" />
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이전"
android:textSize="20dp" />
</LinearLayout>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
package com.example.myapplication;
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;
Button btnGo, btnBack;
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edtUrl);
btnGo = (Button) findViewById(R.id.btnGo);
btnBack = (Button) findViewById(R.id.btnBack);
web = (WebView) findViewById(R.id.webView1);
web.setWebViewClient(new CookWebViewClient());
WebSettings webSet = web.getSettings();
webSet.setBuiltInZoomControls(true);
webSet.setJavaScriptEnabled(true);
btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
web.loadUrl(editText.getText().toString());
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
web.goBack();
}
});
}
class CookWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
[실행결과]
'개발일지 > 안드로이드 JAVA' 카테고리의 다른 글
[안드로이드 프로그래밍 7판] 실습 7-1 배경색 바꾸기 앱 만들기 (0) | 2023.02.23 |
---|---|
firebase로 안드로이드 스튜디오 연결하기 [2탄] (0) | 2023.02.10 |
firebase로 안드로이드 스튜디오 연결하기 [1탄] (0) | 2023.02.07 |
[안드로이드 프로그래밍 7판] 예제 6-15,16 ViewFlipper (0) | 2023.01.31 |
[안드로이드 프로그래밍 7판] 실습 6-1 날짜/시간 예약 앱 만들기 (0) | 2023.01.31 |