반응형

EditText에 주소를 입력 후, 엔터를 치거나 Go버튼을 누르면 해당 사이트로 이동합니다.

이동 후  주소를 EditText에 출력해줍니다.


layout-2014-07-09-205432.png




main.xml


<?xml version="1.0" encoding="utf-8"?>


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

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

             android:orientation="vertical"

             android:layout_width="match_parent"

             android:layout_height="match_parent"

             tools:context=".mainActivity">



   <LinearLayout

           android:orientation="horizontal"

           android:layout_width="fill_parent"

           android:layout_height="wrap_content">


       <EditText

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_weight="3"

               android:maxLines="1"

               android:singleLine="true"

               android:inputType="textNoSuggestions"

               android:selectAllOnFocus="true"

               android:id="@+id/url"/>


       <Button

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_weight="1"

               android:text="Go"

               android:id="@+id/go"/>

   </LinearLayout>



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

             android:id="@+id/webview"

             android:layout_width="match_parent"

             android:layout_height="match_parent" />


</LinearLayout>




mainActivity.java


package com.webnautes.WebView_test;


import android.app.Activity;

import android.content.Intent;

import android.content.res.Configuration;

import android.graphics.Bitmap;

import android.net.Uri;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.WindowManager;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

import android.widget.EditText;


/**

* Created by webnautes on 2014-07-07.

*/

public class mainActivity extends Activity {

   private EditText url_String;

   private WebView webView;


   @Override

   public void onConfigurationChanged(Configuration newConfig){

       super.onConfigurationChanged(newConfig);

   }


   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);


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

       Button button = (Button)findViewById(R.id.go);

       url_String = (EditText) findViewById( R.id.url);



       webView.getSettings().setJavaScriptEnabled(true);

       webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);


       webView.setWebViewClient(new WebViewClient() {

           @Override

           public boolean shouldOverrideUrlLoading(WebView view, String url) {

               view.loadUrl(url);

               return true;

           }


           @Override

           public void onPageStarted (WebView view, String url, Bitmap favicon){

               String urlString = webView.getUrl().toString();

               url_String.setText(urlString);

           }

        });

       webView.loadUrl("http://daum.net");




       button.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {

               String urlString = url_String.getText().toString();


               if ( urlString.startsWith("http") != true )

                  urlString = "http://"+urlString;


               webView.loadUrl(urlString);

           }

       });



       url_String.setOnKeyListener(new View.OnKeyListener(){

           @Override

           public boolean onKey(View v, int keyCode, KeyEvent event) {

               if ( keyCode == KeyEvent.KEYCODE_ENTER )

               {

                   String urlString = url_String.getText().toString();


                   if ( urlString.startsWith("http") != true )

                       urlString = "http://"+urlString;


                   webView.loadUrl(urlString);

                   return true;

               }


               return false;

           }

       });

   }


}



AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>

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

         package="com.webnautes.WebView_test">


   <uses-sdk android:minSdkVersion="18"/>

   <uses-permission android:name="android.permission.INTERNET" />

   <application

           android:icon="@drawable/icon"

           android:label="@string/app_name">


       <activity android:name=".mainActivity"

                  android:configChanges="orientation|keyboardHidden|screenSize">

           <intent-filter>

               <action android:name="android.intent.action.MAIN"/>

               <category android:name="android.intent.category.LAUNCHER"/>

           </intent-filter>

       </activity>

   </application>

</manifest>







반응형

포스트 작성시에는 문제 없었지만 이후 문제가 생길 수 있습니다.
질문을 남겨주면 가능한 빨리 답변드립니다.

여러분의 응원으로 좋은 컨텐츠가 만들어집니다.
지금 본 내용이 도움이 되었다면 유튜브 구독 부탁드립니다. 감사합니다 ~~

유튜브 구독하기


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts