Örneğimizde Android Java ile eklediğimiz bir button denetimine tıkladığımızda TextView’ de istediğimiz bir metni yazdıracağız.
Örneğimizde ek olarak ilk tıklamada farklı, diğer tıklamada farklı metinler görüntüleyeceğiz.

Alttaki kodlar oluşturulduğunda yukarıdaki görüntüyü elde edeceğiz.
activity_main.xml Kodları:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?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/txtMesaj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="..." android:textSize="30dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.233" /> <Button android:id="@+id/btnMesaj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Tıkla" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtMesaj" app:layout_constraintVertical_bias="0.099" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java Kodları:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.example.ilkuygulama; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } boolean kontrol=true; public void onClick(View view) { TextView text=(TextView)findViewById(R.id.txtMesaj); if(kontrol){ text.setText("yazilimkodlama.com "); text.setTextColor(Color.RED); kontrol=false; } else{ text.setText("Android Örnekleri"); text.setTextColor(Color.BLUE); kontrol=true; } } } |