관리 메뉴

moozi

웹뷰예제 본문

안드로이드개발강좌

웹뷰예제

moozi 2015. 10. 12. 16:55
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>


<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="URL을 입력하세요!"
android:id="@+id/editText" />

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이전"
android:id="@+id/button2" />
</LinearLayout>

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView" />
</LinearLayout>
 
 
 

--------------------------------------------------------------------------

 
package com.naver.combo2b_17;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 wv;

// inner class. 웹뷰에 추가기능 부여. 링크를 클릭했을 때 웹뷰내부에서 로딩 가능하도록 설정.
class MyWVC extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// id로 뷰를 찾아오기
edtUrl=(EditText)findViewById(R.id.editText);
btnGo=(Button)findViewById(R.id.button);
btnBack=(Button)findViewById(R.id.button2);
wv=(WebView)findViewById(R.id.webView);

wv.setWebViewClient(new MyWVC());//웹뷰클라이언트 세팅
WebSettings ws=wv.getSettings(); //웹세팅생성
ws.setBuiltInZoomControls(true); //확대축소기능추가
ws.setJavaScriptEnabled(true);//자바스크립트가능하게 추가

//이벤트처리
btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wv.loadUrl(edtUrl.getText().toString());// url을 이용해서 페이지로딩
}
});

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wv.goBack();//이전페이지로 이동
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

'안드로이드개발강좌' 카테고리의 다른 글

sqlite database browser 다운로드  (0) 2015.10.27
다이얼로그 관련 예제  (0) 2015.10.26
ActionBar예제  (0) 2015.10.01
jsp json 활용 샘플  (0) 2015.06.03
jsp post 처리  (0) 2015.05.12
Comments