Android中webview的简单使用

效果如图:
在这里插入图片描述
老规矩,最后有源码。
步骤:
1.在大管家文件中添加网络权限

在这里插入图片描述
2.超简单的webview的实现:三行
在这里插入图片描述
代码如下:
布局文件:

<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文件:

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;
        }
    }

}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值