* MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText editText;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
        webView = findViewById(R.id.webView);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editText.getText().toString().trim();

                if (url != null && !url.isEmpty()) {
                    webView.loadUrl(url);
                } else {
                    Toast.makeText(getApplicationContext(), "Url을 입력하세요.", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

}

* activity_main.xml

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="http://m.naver.com" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="열기" />

    <!-- widget -->
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

* AndroidMenifest.xml

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

<application android:usesCleartextTraffic="true">

* app

minSdkVersion 21

'android' 카테고리의 다른 글

키패드 닫기  (0) 2020.02.03
SeekBar 사용법  (0) 2020.02.03
Spinner 사용법  (0) 2020.02.03
RecyclerView 리스트(문자목록, 통화목록 ..)  (0) 2020.02.03
레이아웃 사용자정의 사용  (0) 2020.02.03
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,