간단한 브라우저 만들기

2019. 3. 19. 16:17Andriod

# *.xml

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

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/edtUrl"
android:layout_width="wrap_content"
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="이동"/>
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이전"/>
</LinearLayout>

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</LinearLayout>


# .java

package com.example.a0319_04;

import android.support.v7.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 edtUrl;
Button btnGo,btnBack;
WebView web;

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

edtUrl = (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 WebViewClient());

WebSettings webset = web.getSettings();
webset.setBuiltInZoomControls(true);

btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
web.loadUrl(edtUrl.getText().toString());
}
});// btnGo

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
web.goBack();
}
});//btnBack
}

class WebViewClient extends android.webkit.WebViewClient{

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}//Web
}



#### AndroidManifest.xml에서  다음 문구를 추가