IT/Android

Widget - WebView

바바옄 2015. 6. 17. 13:45
반응형

웹 프로젝트를 가장 간단하게 모바일로 표현할 수 있는 방법

1. WebView

MainActicity.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
package com.ktds.mywebview;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
 
 
public class MainActivity extends ActionBarActivity {
 
    private WebView webView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl("http://m.naver.com");
 
        WebSettings webSettings = webView.getSettings();
        // 모바일에서 자바스크립트를 실행 시키기 위한 용도
        webSettings.setJavaScriptEnabled(true);
    }
 
    private class MyWebViewClient extends WebViewClient{
 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            view.loadUrl(url);
            return true;
        }
    }
 
 
}
 
cs

 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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">
 
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_gravity="center_horizontal" />
 
</LinearLayout>
 
cs

 

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ktds.mywebview" >
 
    <!-- application에서 인터넷을 사용하기 위해 permission을 받는다.-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
 
cs

 

 

2. WebView

 

MainActicity.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.ktds.mywebview2;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
 
public class MainActivity extends ActionBarActivity {
 
    private WebView webView;
    private EditText etUrl;
    private Button btnGo, btnGoBack, btnGoForward;
    private long backPressedTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        webView = (WebView) findViewById(R.id.webView);
        etUrl = (EditText) findViewById(R.id.etUrl);
        btnGo = (Button) findViewById(R.id.btnGo);
 
        btnGoBack = (Button) findViewById(R.id.btnGoBack);
        btnGoBack.setOnTouchListener(new Button.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                webView.goBack();
                return true;
            }
        });
 
        btnGoForward = (Button) findViewById(R.id.btnGoForward);
        btnGoForward.setOnTouchListener(new Button.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                webView.goForward();
                return true;
            }
        });
 
        webView.setWebViewClient(new MyWebVeiwClient());
        webView.loadUrl("http://m.naver.com");
 
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
 
        btnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = etUrl.getText().toString();
                webView.loadUrl(url);
 
            }
        });
    }
 
    // 안드로이드 단말에서 "뒤로가기" 버튼을 터치했을때, 실행된다.
    @Override
    public void onBackPressed() {
 
        if(backPressedTime == 0L){
            webView.goBack();
            backPressedTime = System.currentTimeMillis();
            //Toast.makeText(this,"아무런 반응을 하지 않는다.",Toast.LENGTH_LONG).show();
        }
        else{
            long currentTime = System.currentTimeMillis();
            if((currentTime-backPressedTime)<=2000){
                Toast.makeText(this,"종료됩니다.!!", Toast.LENGTH_LONG).show();
                finish();
            }
            else{
                backPressedTime = 0L;
            }
        }
    }
 
    private class MyWebVeiwClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
 
}
 
cs

activity_main.xml

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
59
<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"
    android:weightSum="1">
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
            <EditText
                android:id="@+id/etUrl"
                android:layout_weight="10"
                android:hint="URL..."
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/btnGo"
                android:text="GO"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
 
    </LinearLayout>
 
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:longClickable="false"/>
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
        <Button
            android:id="@+id/btnGoBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="뒤로가기"
            />
        <Button
            android:id="@+id/btnGoForward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="앞으로 가기"
            />
 
    </LinearLayout>
</LinearLayout>
 
cs

 

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ktds.mywebview2" >
 
    <uses-permission android:name="android.permission.INTERNET"/>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
 
cs

 

 

            

 

반응형

'IT > Android' 카테고리의 다른 글

ListView  (0) 2015.06.18
Container - TabHost  (0) 2015.06.18
Widget - Layout  (0) 2015.06.17
Widget - ProgressBar  (0) 2015.06.17
Widget - ImageView  (0) 2015.06.16