Senin, 16 Januari 2023

Latihan Membuat Set Dan Get Pada Android

 





Activity main:

<?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:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:padding="10dp"
android:text="Nanda Saputra"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBaseline_toTopOf="parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />

<EditText
android:id="@+id/txtNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nama : "
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="50dp"
/>
<EditText
android:id="@+id/txtJurusan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jurusan : "
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="100dp"
/>
<EditText
android:id="@+id/txtkelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="kelas : "
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="150dp"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:layout_marginRight="50dp"
android:onClick="cek"
android:text="Simpan"
app:backgroundTint="#FF5722"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="OnClick" />




<TextView
android:id="@+id/lblNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nama :"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="250dp"
/>
<TextView
android:id="@+id/lblJurusan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jurusan :"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="300dp"
/>
<TextView
android:id="@+id/lblKelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="kelas :"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="350dp"
/>



</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.EditText;
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 cek(View view) {
EditText Nama = findViewById(R.id.txtNama);
TextView setNama = findViewById(R.id.lblNama);

EditText Jurusan = findViewById(R.id.txtJurusan);
TextView setJurusan = findViewById(R.id.lblJurusan);

EditText Kelas = findViewById(R.id.txtkelas);
TextView setKelas = findViewById(R.id.lblKelas);

String getNama =Nama.getText().toString();
String getJurusan =Jurusan.getText().toString();
String getKelas =Kelas.getText().toString();

setNama.setText(getNama);
setJurusan.setText(getJurusan);
setKelas.setText(getKelas);

}
}



0 komentar:

Posting Komentar