android kotlin 简单的跳转html页面

android kotlin 简单的跳转html页面

  • 布局页面

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".ServiceActivity">
    
    
       <!-- &lt;!&ndash; 获取网站的标题&ndash;&gt;
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>
    
        &lt;!&ndash;开始加载提示&ndash;&gt;
        <TextView
            android:id="@+id/text_beginLoading"
            android:layout_below="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>
    
        &lt;!&ndash;获取加载进度&ndash;&gt;
        <TextView
            android:layout_below="@+id/text_beginLoading"
            android:id="@+id/text_Loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>
    
        &lt;!&ndash;结束加载提示&ndash;&gt;
        <TextView
            android:layout_below="@+id/text_Loading"
            android:id="@+id/text_endLoading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>-->
    
        <!--显示网页区域-->
        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp" />
    </RelativeLayout>
    
  • 后端布局

    package cn.tdtd.work
    
    import android.annotation.SuppressLint
    import android.os.Bundle
    import android.webkit.WebView
    import android.webkit.WebViewClient
    import android.widget.Toast
    import cn.tdtd.work.common.MyActivity
    
    
    class ServiceActivity : MyActivity() {
    
        private lateinit var webView: WebView
        private var exitTime: Long = 0
    
        @SuppressLint("SetJavaScriptEnabled")
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_service)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
            
            webView = WebView(this)
            webView.webViewClient = object : WebViewClient() {
                //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
                override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
                    view.loadUrl(url)
                    return true
                }
            }
            webView.settings.javaScriptEnabled = true //设置WebView属性,运行执行js脚本
            webView.loadUrl("http://baidu.com")//调用loadUrl方法为WebView加入链接
            
            setContentView(webView) //调用Activity提供的setContentView将webView显示出来
        }
    
    
        //我们需要重写回退按钮的时间,当用户点击回退按钮:
        //1.webView.canGoBack()判断网页是否能后退,可以则goback()
        //2.如果不可以连续点击两次退出App,否则弹出提示Toast
        override fun onBackPressed() {
            if (webView.canGoBack()) {
                webView.goBack()
            } else {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    Toast.makeText(applicationContext, "再按一次退出程序",
                        Toast.LENGTH_SHORT).show()
                    exitTime = System.currentTimeMillis()
                } else {
                    super.onBackPressed()
                }
            }
        }
    
        override fun onSupportNavigateUp(): Boolean {
            finish()
            return super.onSupportNavigateUp()
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值