본문 바로가기

Android

기타위젯 - WebView

activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout2"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <EditText

            android:id="@+id/et"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:hint="http://"

            android:inputType="textUri" />

 

        <Button

            android:id="@+id/btn1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="3"

            android:text="이동" />

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/btn2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="이전" />

 

        <Button

            android:id="@+id/btn3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="다음" />

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/btn4"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="로컬 파일 읽기" />

    </LinearLayout>

 

    <WebView

        android:id="@+id/webview"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</LinearLayout>

 

MainActivity.java

 

     package com.example.webviewex01;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.content.Context;

import android.view.Menu;

import android.view.View;

import android.view.inputmethod.InputMethod;

import android.view.inputmethod.InputMethodManager;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

     WebView webview;

     InputMethodManager imm;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

 

           webview = (WebView) findViewById(R.id.webview);

           WebSettings webSettings = webview.getSettings();

           webSettings.setSavePassword(false);

           webSettings.setSaveFormData(false);

           webSettings.setSupportZoom(true);

           webSettings.setJavaScriptEnabled(true);

 

           webview.setWebViewClient(new MyWebClient());

           webview.loadUrl("http://m.daum.net");

 

           findViewById(R.id.btn1).setOnClickListener(clickListener);

           findViewById(R.id.btn2).setOnClickListener(clickListener);

           findViewById(R.id.btn3).setOnClickListener(clickListener);

           findViewById(R.id.btn4).setOnClickListener(clickListener);

     }

 

     private View.OnClickListener clickListener = new View.OnClickListener() {

 

           @Override

           public void onClick(View v) {

                // TODO Auto-generated method stub

                if (v.getId() == R.id.btn1) {

                     EditText et = (EditText) findViewById(R.id.et);

                     Toast.makeText(MainActivity.this, et.getText(), Toast.LENGTH_SHORT).show();

                     String url = et.getText().toString();

                    

                     webview.loadUrl(url);

 

                     imm.hideSoftInputFromWindow(et.getWindowToken(), 0);

                } else if (v.getId() == R.id.btn2) {

                     if (webview.canGoBack())

                           webview.goBack();

                } else if (v.getId() == R.id.btn3) {

                     if (webview.canGoForward())

                           webview.goForward();

                } else if (v.getId() == R.id.btn4) {

                     webview.loadUrl("file:///android_asset/test.html");

                }

           }

     };

 

     final class MyWebClient extends WebViewClient {

 

           @Override

          public boolean shouldOverrideUrlLoading(WebView view, String url) {

                // TODO Auto-generated method stub

                super.shouldOverrideUrlLoading(view, url);

                view.loadUrl(url);

                return true;

           }

 

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}






 

'Android' 카테고리의 다른 글

대화상자 - AlertDialog  (0) 2013.04.02
대화상자 - Dialog  (0) 2013.04.02
기타위젯 - SlidingDrawer  (0) 2013.04.02
기타위젯 - AutoCompleteTextView  (0) 2013.04.02
날짜와 시간 - Chronometer  (0) 2013.04.02