Android : Belajar Kotlin Untuk Pemula Part-3


Ini adalah postingan pertama saya di tahun 2020, sudah lama tidak update tulisan di blog ini, pada postingan kali ini saya akan sharing tentang function didalam kotlin, langsung saja, pada activity main layout seperti berikut  


<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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">

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_centerInParent="true"        >

        <EditText            android:id="@+id/editText1"            android:hint="Input 1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:inputType="number">

        </EditText>

        <EditText            android:id="@+id/editText2"            android:hint="Input 2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:inputType="number">

        </EditText>

        <Button            android:id="@+id/btnSUM"            android:hint="+"            android:layout_width="match_parent"            android:layout_height="wrap_content">

        </Button>
    </LinearLayout>
</RelativeLayout>


Lalu pada MainActivity.kt seperti berikut  :


package com.ketik.toekang.kotlin_function

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import android.widget.Toast.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    
    // Deklarasi Function
    fun sumNumber(a: Int, b:Int):Int{
        return a + b
    }

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

        btnSUM.setOnClickListener {            Toast.makeText(this, ""+sumNumber(editText1.text.toString().toInt(),editText2.text.toString().toInt()),
                Toast.LENGTH_SHORT).show()
        }    }
}


Jika sudah silahkan dicoba jalankan aplikasinya, sekian sharing singkat ini semoga bermanfaat.

Comments

Popular posts from this blog

IT Asset Management Dengan PHP MySQL

PHP MySql CRUD Dengan Konsep MVC

Cara Sederhana Multi Insert Data Dengan PHP - MySQL