Android中webview的簡單使用

效果如圖:
在這裏插入圖片描述
老規矩,最後有源碼。
步驟:
1.在大管家文件中添加網絡權限

在這裏插入圖片描述
2.超簡單的webview的實現:三行
在這裏插入圖片描述
代碼以下:
佈局文件:
java

<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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <Button
            android:id="@+id/passon"
            android:layout_width="61dp"
            android:layout_height="36dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:text="轉入" />

        <Button
            android:id="@+id/themain"
            android:layout_width="212dp"
            android:layout_height="37dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="0dp"
            android:text="首頁" />

        <EditText
            android:id="@+id/myurl"
            android:layout_width="320dp"
            android:layout_height="45dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="0dp"
            android:layout_marginTop="0dp"
            android:ems="10"
            android:hint="請輸入要轉入的網址"
            android:maxLines="1"
             />
    </RelativeLayout>
    <WebView
        android:id="@+id/myweb"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>
    </LinearLayout>

java文件:android

public class MainActivity extends Activity implements View.OnClickListener {


    private WebView myweb;
    private String baidu = "http://www.baidu.com";
    private Button passon;
    private Button themain;
    private EditText myurl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        myweb.getSettings().setJavaScriptEnabled(true);//調用getSettings()方法添加屬性使webview支持JavaScript的腳本
        myweb.setWebViewClient(new WebViewClient());//使其跳轉後依然使用webview來顯示
        myweb.loadUrl(baidu);//利用loadUrl()顯示網址


    }


    private void initView() {
        myweb = (WebView) findViewById(R.id.myweb);
        passon = (Button) findViewById(R.id.passon);
        passon.setOnClickListener(this);
        themain = (Button) findViewById(R.id.themain);
        themain.setOnClickListener(this);
        myurl = (EditText) findViewById(R.id.myurl);
        myurl.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.passon://轉入按鈕
                String passonUri =myurl.getText().toString();
                if (!passonUri.equals("")){
                    myweb.loadUrl(passonUri);
                }else{
                    Toast.makeText(this,"請輸入網址",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.themain://首頁按鈕
                myweb.loadUrl(baidu);
                break;
        }
    }

}

本文同步分享在 博客「計蒙不吃魚」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。web

相關文章
相關標籤/搜索