Senin, 30 Januari 2023

Belajar Radiogroup dan checkbox

Activity Diagaram

<?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"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:text="FORM INPUT"
android:textAlignment="center"
android:textSize="30dp"
android:background="@color/purple_500"
android:textColor="@color/white"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="50dp"
android:orientation="vertical"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NO.ID"
app:layout_constraintTop_toTopOf="@+id/txtTime"
android:layout_marginTop="5dp"
/>

<EditText
android:id="@+id/txtId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="INPUT NO ID"
android:inputType="number"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NAMA"
app:layout_constraintTop_toTopOf="@+id/txtId"
android:layout_marginTop="5dp"
/>

<EditText
android:id="@+id/txtNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="INPUT NAMA"
android:inputType="text"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EMAIL"
app:layout_constraintTop_toTopOf="@+id/txtNama"
android:layout_marginTop="5dp"
/>

<EditText
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="INPUT EMAIL"
android:inputType="textEmailAddress"
/>

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hobi"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="260dp"
/>
<LinearLayout
android:id="@+id/lnHobi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="280dp">
<CheckBox
android:id="@+id/chcSilat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Silat"/>


<CheckBox
android:id="@+id/chcKarate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Karate"/>

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jenis Kelamin"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="330dp"
/>
<RadioGroup
android:id="@+id/rgJk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="350dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rdLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Laki-Laki"/>


<RadioButton
android:id="@+id/rdPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perempuan"/>
</RadioGroup>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SIMPAN"
app:layout_constraintTop_toTopOf="@id/rgJk"
android:layout_marginTop="50dp"
android:textSize="20dp"
android:onClick="simpan"
tools:ignore="OnClick" />
<TextView
android:id="@+id/lblId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="480dp"/>

<TextView
android:id="@+id/lblNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="NAMA"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="@+id/lblId"
android:layout_marginTop="40dp"/>

<TextView
android:id="@+id/lblEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EMAIL"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="@+id/lblNama"
android:layout_marginTop="40dp"/>

<TextView
android:id="@+id/lblHobi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HOBI"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="@+id/lblEmail"
android:layout_marginTop="40dp"/>


<TextView
android:id="@+id/lblJk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Jenis Kelamin"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="@+id/lblHobi"
android:layout_marginTop="40dp"/>


</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity;
package com.example.rpl1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

public void simpan(View view) {
EditText No = findViewById(R.id.txtNo);
TextView setNO = findViewById(R.id.lblNO);
EditText Nama = findViewById(R.id.txtNama);
TextView setNama = findViewById(R.id.lblNama);
EditText Email = findViewById(R.id.txtEmail);
TextView setEmail = findViewById(R.id.lblEmail);
TextView setJK = findViewById(R.id.lblJK);
CheckBox Silat = findViewById(R.id.chcSilat);
TextView setSilat = findViewById(R.id.lblHobi);
CheckBox Karate = findViewById(R.id.chcKarate);
TextView setKarate = findViewById(R.id.lblHobi);
CheckBox Hapkido = findViewById(R.id.chcHapkido);
TextView setHapkido = findViewById(R.id.lblHobi);
RadioGroup JK = findViewById(R.id.rdJK);


int jk = JK.getCheckedRadioButtonId();
Button Gender = findViewById(jk);
String gender = Gender.getText().toString();
String getNama = Nama.getText().toString();
String getNo = No.getText().toString();
String getEmail = Email.getText().toString();
String getSilat = Silat.getText().toString();
String getKarate = Karate.getText().toString();
String getHapkido = Hapkido.getText().toString();

setNama.setText(getNama);
setNO.setText(getNo);
setEmail.setText(getEmail);
setJK.setText(gender);
setSilat.setText(getSilat);
setKarate.setText(getKarate);
setHapkido.setText(getHapkido);
}
}