Program Android: Membuat Login Dengan Kotlin

Baringin
By -
0




Pengantar

 Apa anda sudah membahas sebebelumnya tentang Perbabangan, Sekarang mari kita praktekkan di dalam kasus pembuatan login:

    Buatlah projek baru dengan nama form_login.

Buat activity varu dengan cara klik kanan pada App-> New->Activity->Empty Views Activity dan beri nama "Home".

Buka Activity_Main dan Buatlah user Interface seperti berikut:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:padding="15dp"
tools:context=".MainActivity">

<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:background="#6FA8D6"
tools:layout_editor_absoluteX="6dp"
tools:layout_editor_absoluteY="4dp" />

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="SELAMAT DATANG"
android:textAlignment="center"
android:textColor="#D30B0B"
android:textSize="40sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="#43DA1B"
android:padding="10dp"
android:text="Silakah Login"
android:textColor="#FCFCFC"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/txtEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Input Username"
android:inputType="text"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:layout_editor_absoluteX="16dp" />

<EditText
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/txtEdit"
tools:layout_editor_absoluteX="16dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:onClick="Login"
android:text="Log In"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtPassword" />

</androidx.constraintlayout.widget.ConstraintLayout>


Buat script programnya:

package com.example.mi_2

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {
val User = "user"
val Pass = "user"

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

fun Login(view: View) {
val user = findViewById<EditText>(R.id.txtEdit)
val pass = findViewById<EditText>(R.id.txtPassword)

val getUser = user.getText().toString()
val getPass = pass.getText().toString()

//cek user
if(User.equals(getUser)){
//cek pass
if(Pass.equals(getPass)){
//Masuk ke home
val i = Intent(this, Home::class.java)
startActivity(i)
}else{
Toast.makeText(this, "Password Salah", Toast.LENGTH_SHORT).show()
}
}else{
Toast.makeText(this, "User Salah", Toast.LENGTH_SHORT).show()
}
}
}

Terakhir jalankan progam. 

Post a Comment

0Comments

Post a Comment (0)