[안드로이드,Android]WebView,웹뷰

2013. 2. 5. 09:47Mobile/Android



반응형

 

package com.example.ggari_webview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class MainActivity extends Activity {
	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 WebClient()); // 응룡프로그램에서 직접 url 처리
		WebSettings set = webview.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		webview.loadUrl("http://www.google.com");
		
		findViewById(R.id.btnStart).setOnClickListener(onclick);
	}
	
	OnClickListener onclick =new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			System.out.println("클릭");
			String url= null;
			EditText add = (EditText)findViewById(R.id.add);
			url = add.getText().toString();
			webview.loadUrl(url);			
		}
	};
	
	class WebClient extends WebViewClient {
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="http://www.google.com" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnStart"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="이동" />
    </LinearLayout>

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="true"
        android:focusableInTouchMode="true" />
</LinearLayout>
AndroidManifest.xml
 
반응형