EditText에 적힌 주소로 WebView가 접속하여 출력해주는 간단한 어플리케이션
앞에 http://를 적지 않아도 실행 되도록 설정.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.webbrower; 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 { 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 CookWebViewClient()); WebSettings webSet = web.getSettings(); webSet.setBuiltInZoomControls(true); btnGo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String strTemp = edtUrl.getText().toString(); if(!strTemp.substring(0,7).equals("http://")){ strTemp= "http://"+strTemp; } web.loadUrl(strTemp); } }); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { web.goBack(); } }); } public class CookWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url); } } } | cs |
AndroidManifest.xml 에서
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
를 명시해주어야 어플내에 인터넷을 사용할 수 있다.
activity_main.xml
123456789101112131415161718192021222324252627282930313233343536 <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"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid:id="@+id/edtUrl"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:singleLine="true"/><Buttonandroid:id="@+id/btnGo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="이동"/><Buttonandroid:id="@+id/btnBack"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="이전"/></LinearLayout><WebViewandroid:id="@+id/webView1"android:layout_width="match_parent"android:layout_height="match_parent"></WebView></LinearLayout>cs
'프로그래밍 > Java, Android' 카테고리의 다른 글
(Java) 제네릭 (0) | 2016.07.10 |
---|---|
(Java) 파일 입력과 출력 - FileInputStream, FileOutputStream (0) | 2016.07.10 |
(안드로이드) 제목창, 타이틀 없애는 방법 (0) | 2016.07.10 |
(안드로이드) 기본 갤러리에 이미지를 불러오는 Simple Application (0) | 2016.07.10 |
(Java) Thread를 이용한 심플 경마 (0) | 2016.07.10 |