초급 8.심플 웹 브라우저

mangyun·2021년 11월 24일
0

초급 앱개발

목록 보기
8/8

정리

UI

  • 입력
    tools:ignore="ContentDescription" //무시 - 어떤 동작을 하는건지 설명
    android:importantForAutofill="no" //자동완성관련 x
    android:inputType="textUri"//주소를 치기 쉬운 키보드
  • 액션버튼을 누르면 키보드가 내려감
    android:imeOptions="actionDone"

  • 리플 이펙트
    android:background="?attr/selectableItemBackground"

    • 리플 이펙트시 좁아지기 때문에 가로, 세로 비율로 크기 키우기
      app:layout_constraintDimensionRatio="1:1"
  • drawable 리소스

<solid android:color="@color/light_gray"/> //색칠
<corners android:radius="16dp"/> //끝을 둥글게
  • 주소와 웹 구분
    android:elevation="4dp" //그림자로 띄워서 구분
    android:background="@color/white" //원래는 투명하므로, 흰색처리

  • 새로고침

    1. gradle의 dependencies에 추가
      implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    2. webView를 refreshLayout에 넣어야함

      <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/refreshLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolBar">
            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
      
    3. 새로고침 표시 제거

    inner class WebViewClient : android.webkit.WebViewClient() {//inner로 상위 클래스 접근가능
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)

            refreshLayout.isRefreshing = false
        }
    }

권한

  • manifests에 인터넷 권한 설정
<uses-permission android:name="android.permission.INTERNET"/>

보안

  • http로는 보안으로 인해 접근 못하지만, manifest에 밑 코드를 추가하면 됨
    android:usesCleartextTraffic="true"

  • 자바스크립트의 보안으로 인해 webView에 추가해야 사이트 기능을 이용할 수 있음
    webView.settings.javaScriptEnabled = true (apply 적용)

로드

webView.webViewClient = WebViewClient() // webView에 load하게 함

addressBar.setOnEditorActionListener { v, actionId, event ->//주소창 입력액션
            if (actionId == EditorInfo.IME_ACTION_DONE) {//키보드가 닫히면
                webView.loadUrl(v.text.toString())//해당 사이트로 이동
            }
            return@setOnEditorActionListener false //true를 반환하게된다면 액션을 진행할 수가 없어서 키보드가 안닫아짐
        }
  • 이전 페이지로
 goBackButton.setOnClickListener {
            webView.goBack() 
        }
  • 앞 페이지로

           goForwardButton.setOnClickListener {
               webView.goForward() 
    
           }
  • 뒤로가기 버튼 함수
    override fun onBackPressed() {
        if(webView.canGoBack()){ //이전 페이지가 있다면
            webView.goBack()
        }
        else{ //없다면
            super.onBackPressed() // 앱종료
        }
    }
    

기타

  • 주소 상수화
    companion object {
        private const val DEFAULT_URL = "http://www.google.com"
    }
profile
기억보다는 기록을 하자.

0개의 댓글