- 閱讀此文需要對Kotlin基本語法有所了解。
- 本文只是一個Kotlin簡單開發(fā)Android Demo入手的文章,不對Kotlin語法特點進行探討。
此Demo用到Kotlin基本知識點回顧:
一.定義變量:
var:修飾可變變量,如:var a : Int = 0 or var textView : TextView ?= null
val:修飾常量,如:val b = 1 【此后b不能再被重新賦值】
二.伴生對象的使用:
語法特點:
companion object {
...
//此方法可用來定義java中 靜態(tài)常量等【此Demo用到】
//val WEB_URL="http://www.baidu.com"
// 類似于在java類中直接定義的常量 static final String WEB_URL="http://www.baidu.com"
}
三.擴展函數(shù)【不擴展說明】
四.語法特點:
var textView : TextView ?= null
textView=findViewById(R.id.u_text_id) as TextView
textView?.text?:"null"
- 上述代碼第三行代碼類似于 Java 中
textView==null?“null”:textView.getText().toString().isEmpty()?"null" :textView.getText().toString();
開始寫Android小Demo
1.用AS創(chuàng)建我們的項目取名為WebViewByKotlin:
- 修改app目錄下的buid.gradle,給項目添加依賴:
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
dependencies {
. . .
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
- 修改整個項目的buid.gradle,加入如下依賴包:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
2.開始寫activity_main.xml 代碼如下:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/web_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3.寫MainActivity.kt代碼:
class MainActivity : AppCompatActivity() {
//定義變量
private var mWebView: WebView? = null
private var mExitTime = 0L
companion object {
// 定義WebView首頁地址[伴生對象]
//定義static final
val WEB_URL = "http://www.baidu.com"
val TAG=MainActivity::class.simpleName //定義Log的TAG
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar!!.hide() //隱藏ActionBar
initAndSetupView()
}
// 初始化對象
fun initAndSetupView() {
val webViewContainer = findViewById(R.id.web_view_container) as FrameLayout
val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
mWebView = WebView(applicationContext)
webViewContainer.addView(mWebView, params)
var webSettings = mWebView!!.settings
webSettings.javaScriptEnabled = true
webSettings.javaScriptCanOpenWindowsAutomatically = true
webSettings.allowFileAccess = true// 設置允許訪問文件數(shù)據(jù)
webSettings.setSupportZoom(true)//支持縮放
webSettings.javaScriptCanOpenWindowsAutomatically = true
webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
webSettings.domStorageEnabled = true
webSettings.databaseEnabled = true
mWebView!!.setOnKeyListener(OnKeyEvent)
mWebView!!.setWebViewClient(webClient)
mWebView!!.loadUrl(WEB_URL)
}
private val webClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
Log.d(TAG, url)
return false
}
}
private val OnKeyEvent = View.OnKeyListener { v, keyCode, event ->
val action = event.action
val webView = v as WebView
if (KeyEvent.ACTION_DOWN == action && KeyEvent.KEYCODE_BACK == keyCode) {
if (webView?.canGoBack()) {
webView.goBack()
return@OnKeyListener true
}
}
false
}
override fun onBackPressed() {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
showToast("連按兩下退出應用")
//showToast("連按兩下退出應用",Toast.LENGTH_SHORT) //此種方法調用也可以 有點可變參數(shù)的意思在里面
mExitTime = System.currentTimeMillis()
} else {
super.onBackPressed()
}
}
override fun onResume() {
super.onResume()
mWebView?.onResume()
}
override fun onPause() {
super.onPause()
mWebView?.onPause()
}
override fun onDestroy() {
super.onDestroy()
mWebView?.clearCache(true)
(mWebView?.parent as FrameLayout).removeView(mWebView)
mWebView?.stopLoading()
mWebView?.setWebViewClient(null)
mWebView?.setWebChromeClient(null)
mWebView?.removeAllViews()
mWebView?.destroy()
mWebView = null
}
//擴展函數(shù)
fun MainActivity.showToast(message: String, length: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, length).show()
}
}
*mWebView?.xxx 操作 代碼驗證后 得出類似于java中
if (mWebView != null) {
mWebView.xxx;
}