顯示具有 Kotlin 標籤的文章。 顯示所有文章
顯示具有 Kotlin 標籤的文章。 顯示所有文章

2018年9月15日 星期六

Android-如何用Kotlin簡易設定與監聽按鈕(Button)

想必各位大大從 Java 轉到 Kotlin 有極大的痛楚
此篇就以現在常遇到的狀況下做簡易的介紹

首先我們先把一般情況下的
先把頁面畫出來





















1 . activity_button.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <Button
        android:id="@+id/buttonTest"
        android:layout_width="88dp"
        android:layout_height="48dp"
        android:layout_marginBottom="232dp"
        android:layout_marginEnd="148dp"
        android:layout_marginStart="148dp"
        android:layout_marginTop="231dp"
        android:text="我是按鈕"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

重點來了
以下是 Java 寫法
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ButtonJavaActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnTest;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnTest = findViewById(R.id.buttonTest);
        btnTest.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buttonTest:
                Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}

以下是 Kotlin 寫法 
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.Toast

class ButtonActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var btnTest: Button;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_button)

        btnTest = findViewById(R.id.buttonTest);
        btnTest.setOnClickListener(this);
        
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.buttonTest -> {
                Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();
            }
            else -> {

            }
        }
    }
}

2018年7月22日 星期日

Kotlin-變數初始值 lateinit 與 ?= 與 lazy

不曉得個外大大在用 Kotlin 開發 Android 時候
是否有發現為啥無法想 Java 定義一個變數就好
反而還要在定義其他的屬性

我們先來看有哪幾種
1 . lateinit
此宣告是指,晚一點初始化
但此變數是不可為 Null 的










2 . ?=
雖然可以為空值
但是審查的條件也變得相對嚴格
要使用底下功能時
都必須要檢查
不然直接報錯










3 .


參考資料:Kotlin —  lateinit vs lazy
參考資料:[android]活动-lateinit VS 任何?= null
參考資料:Kotlin: Lazy 和 Lateinit 的使用
參考資料:Kotlin: When to Use Lazy or Lateinit

2018年6月20日 星期三

2017年12月10日 星期日

Android-Kotlin 監聽元件錯誤

近期用 Kotlin 開發 Android 時
會發現非常奇怪的錯誤會發生
通常是在匯入 interface 的時候
比如 View.OnClickListener...等
在此記下
以便於之後查閱

1 . 程式碼在此
package com.xxx.xxx.xxx.xxx

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        linearLayout.setOnClickListener(this);

    }

    override fun onClick(view: View) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        when (view?.id) {
            R.id.linearLayout -> {
                println("found");
            }
            else -> println("item not found");
        }

    }

}


2 . 導致錯誤...
Process: com.xxx.xxx.xxx.xxx, PID: 8563
kotlin.NotImplementedError: An operation is not implemented: not implemented
    at com.xxx.xxx.xxx.xxx.MainActivity.onClick(MainActivity.kt:19)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22429)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

解法:

去除預設的 TODO("not implemented")
結果程式終於能夠正常執行了

簡單的說法
提醒你要記得實作
但是在 Kotlin 下 TODO 是會起阻擋提醒作用的

參考資料 :
 [Kotlin]kotlin.NotImplementedError: An operation is not implemented: not implemented
NotImplementedError