首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android中webview的简单使用

Android中webview的简单使用

作者头像
计蒙不吃鱼
发布2025-06-10 14:49:19
发布2025-06-10 14:49:19
23600
代码可运行
举报
文章被收录于专栏:Android开发Android开发
运行总次数:0
代码可运行

效果如图:

老规矩,最后有源码。 步骤: 1.在大管家文件中添加网络权限

2.超简单的webview的实现:三行

代码如下: 布局文件:

代码语言:javascript
代码运行次数:0
运行
复制
<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文件:

代码语言:javascript
代码运行次数:0
运行
复制
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;
        }
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档