반응형
|
안드로이드(Android) WebView 브라우저기능중 URL 이동바 구현 |
|
개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2 |
|
이전아티클에서 브라우저를 구현하고 옵션메뉴에서 GO TO 를 클릭하게 되면 사이트를 이동할수 있게 페이지를 하나 만들었습니다. 그러나 이번에는 옵션메뉴를 누르게 되면 화면상단에 URL 을 입력할수 있는 바가 나타나도록 해 보겠습니다. |
구현 화면은 다음과 같습니다. 옵션메뉴중 GO TO 를
누르게 되면 상단에 URL 창이 나오게 되고 GO 를
누르게 되면 화면이 이동하게 되는 것이죠.
먼저 레이아웃부터 보겠습니다.
TableLayout 으로 상단에 URL 입력창을 배치 하였고
처음에는 “gone” 을 사용해 숨깁니다.
<TableLayout
android:id="@+id/UrlEntry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:visibility="gone" >
<TableRow>
<EditText
android:id="@+id/go_url"
android:layout_weight="9"
android:text="http://" />
<Button
android:id="@+id/go_button"
android:layout_weight="1"
android:text="바로가기" />
</TableRow>
</TableLayout>
두번째는 메인 Activity 에 옵션메뉴를 구현합니다.
이 메뉴의 첫번째가 URL 을 입력할수 있는 창을
뜨게 합니다.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_GOTO:
ToggleGotoVisibility();
break;
case MENU_ABOUT:
openAboutDialog();
break;
case MENU_EXIT:
openExitDialog();
break;
case MENU_BACKFORD:
if (myBrowser.canGoBack())
myBrowser.goBack();
break;
case MENU_RELOAD:
myBrowser.reload();
break;
case MENU_FORWARD:
if (myBrowser.canGoForward())
myBrowser.goForward();
break;
}
return true;
}
MENU_GOTO 를 클릭하게 되면 ToogleGotoVisibility()
함수를 수행하게 되는데 함수의 내용은
처음에 레이아웃을 숨겼던 것을 보이게 하는 것입니다.
View.VISIBLE 옵션을 이용해 상단 URL 입력창을
보이게 합니다.
void ToggleGotoVisibility() {
if (myUrlBar.getVisibility() == View.GONE) {
myUrlBar.setVisibility(View.VISIBLE);
} else {
myUrlBar.setVisibility(View.GONE);
}
}
전체 XML 의 내용은 다음과 같습니다.
상단에 URL 입력부분과 WebView 두개로
구분되어 있습니다.
<?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" >
<TableLayout
android:id="@+id/UrlEntry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:visibility="gone" >
<TableRow>
<EditText
android:id="@+id/go_url"
android:layout_weight="9"
android:text="http://" />
<Button
android:id="@+id/go_button"
android:layout_weight="1"
android:text="바로가기" />
</TableRow>
</TableLayout>
<WebView
android:id="@+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
메인 Activity 의 전체 소스입니다.
옵션메뉴 만드는 것과 옵션메뉴클릭시
실행하는 부분으로 되어 있습니다.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
public class SampleActivity6 extends Activity {
final int MENU_GOTO = 0;
final int MENU_ABOUT = 1;
final int MENU_EXIT = 2;
final int MENU_BACKFORD = 3;
final int MENU_RELOAD = 4;
final int MENU_FORWARD = 5;
WebView myBrowser;
TableLayout myUrlBar;
EditText gotoUrl;
Button myUrlButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_activity6);
String myURL = "http://mainia.tistory.com";
myBrowser = (WebView) findViewById(R.id.mybrowser);
myUrlBar = (TableLayout) findViewById(R.id.UrlEntry);
gotoUrl = (EditText) findViewById(R.id.go_url);
myUrlButton = (Button) findViewById(R.id.go_button);
myUrlButton.setOnClickListener(myUrlButtonOnClickListener);
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, MENU_GOTO, 0, R.string.str_Goto);
menu.add(0, MENU_ABOUT, 0, R.string.str_About);
menu.add(0, MENU_EXIT, 0, R.string.str_Exit);
menu.add(0, MENU_BACKFORD, 0, R.string.str_Backward);
menu.add(0, MENU_RELOAD, 0, R.string.str_Reload);
menu.add(0, MENU_FORWARD, 0, R.string.str_Forward);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_GOTO:
ToggleGotoVisibility();
break;
case MENU_ABOUT:
openAboutDialog();
break;
case MENU_EXIT:
openExitDialog();
break;
case MENU_BACKFORD:
if (myBrowser.canGoBack())
myBrowser.goBack();
break;
case MENU_RELOAD:
myBrowser.reload();
break;
case MENU_FORWARD:
if (myBrowser.canGoForward())
myBrowser.goForward();
break;
}
return true;
}
void ToggleGotoVisibility() {
if (myUrlBar.getVisibility() == View.GONE) {
myUrlBar.setVisibility(View.VISIBLE);
} else {
myUrlBar.setVisibility(View.GONE);
}
}
private void openAboutDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.str_About)
.setMessage(R.string.str_about_message)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
}
}).show();
}
private void openExitDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.str_Exit)
.setMessage(R.string.str_exit_message)
.setNegativeButton(R.string.str_no,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
}
})
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
finish();
}
}).show();
}
private Button.OnClickListener myUrlButtonOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
myBrowser.loadUrl(gotoUrl.getText().toString());
}
};
}
반응형
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) 스레드를 이용하여 애니메이션 구현하기 (0) | 2014.10.28 |
|---|---|
| 안드로이드(Android) 간단한 스레드 사용법과 숫자값 올리기 (0) | 2014.10.27 |
| 안드로이드(Android) 아날로그 시계의 시간을 음성(TTS) 으로 알려주기 (3) | 2014.10.27 |
| 안드로이드(Android) 간단하게 TTS(Text-To-Speech) 구현하기 (8) | 2014.10.26 |
| 안드로이드(Android) WebView 이용해 브라우저구현과 메뉴구현 (0) | 2014.10.25 |
| 안드로이드(Android) 스마트 폰에 모든 시스템 정보 알아보기 (1) | 2014.10.25 |
| 안드로이드(Android) CPU 버전 알아오기 (0) | 2014.10.24 |
| 안드로이드(Android) 2014년 9월 까지 플랫폼 버전과 화면해상도 분포 (0) | 2014.10.24 |
