Android App

webViewWithEditText in Android Studio

YunSeong 2021. 8. 2. 13:05
728x90
반응형

 

WebViewWithEditText

 

https://yunseong.tistory.com/entry/Android-App-Study2-sharedPreferences-webView-Navigation

 

Android App Study_2 (sharedPreferences, webView, Navigation)

1. SharedPreferences https://youtu.be/-2QPmS4OWos SharedPreferences는 앱을 끄고 다시 키더라고 정보가 남아있도록 정보를 저장할 수 있는 것이다. 이것을 사용하는 방법은 1 2 3 4 SharedPreferences shared..

yunseong.tistory.com

위 페이지에서 만든 webView에다가 EditText를 이용해서 URL 입력 창을 만들 것이다. 

 

앞에서 webView에 대한 거의 모든 기능들을 구현해놨기 때문에 일단 EditText 위젯을 추가해줬다. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etForURL" />
 
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
 
 
</LinearLayout>
cs

 linearLayout을 이용해서 밑의 사진과 같이 다른 인터넷 브라우저처럼 깔끔하게 위젯을 배치했다. 

 

 

 

 

 

그리고 java파일에 아래와 같은 코드를 추가했다. 

1
2
3
4
5
6
7
8
9
etForURL = (EditText)findViewById(R.id.etForURL);
etForURL.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        url = etForURL.getText().toString();
        webView.loadUrl(url);
        return true;
    }
});
cs

1- EditText 변수에 실제 위젯을 findViewById()을 이용해서 연결해줬다. 

2~9- SetOnEditorActionListener()을 이용해서 EditText 위젯을 편집했을 때 이벤트를 발생시키도록 했다.

5- EditText에서 getText()로 받아온 text를 toString()을 이용해서 string의 형태로 url에 저장했다. 

6- loadUrl을 이용해서 받아온 url의 웹페이지를 webView에 띄우도록 했다.

 

 

 

 

728x90
반응형