Bu örnekte Android editText nesnesine girilen metni sayıya dönüştürüp textView’de nasıl yazdıracağınızı göstereceğiz.
Bu yazıda hem Kotlin hem de Java ile bu sorunun çözümünü bulacaksınız. Kodları yazmaya başlayalım.
Adım 1: Aşağıdaki kodu res/layout/activity_main.xml dosyasına ekleyin.
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 37 38 39 40 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:id="@+id/textSonuc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:background="#008080" android:padding="5dp" android:text="Tasarım Kodlama" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> <EditText android:id="@+id/editSayi1" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <EditText android:id="@+id/editSayi2" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editSayi1" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editSayi2" android:layout_centerInParent="true" android:onClick="btnHesapla" android:text="Tıkla" /> </RelativeLayout> |
Adım 2(Kotlin): Aşağıdaki kodu src/MainActivity.kt‘ye ekleyin.
String türündeki sayıyı int türüne çevirmek için buradaki yazıyı inceleyebilirsiniz.
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 37 38 | package com.example.tasarimkodlama import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.EditText import android.widget.TextView import org.json.JSONException import org.json.JSONObject class MainActivity : AppCompatActivity() { //Eklenen Kodlar. Değişken tanımlandı. lateinit var editSayi1:EditText lateinit var editSayi2:EditText lateinit var textSonuc:TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } fun btnHesapla(view:View){ editSayi1= findViewById(R.id.editSayi1) editSayi2= findViewById(R.id.editSayi2) textSonuc= findViewById(R.id.textSonuc) var sonuc = editSayi1.text.toString().toInt() + editSayi2.text.toString().toInt() textSonuc.setText(sonuc.toString()) } } |
Adım 2(Java): Aşağıdaki kodu src/MainActivity.kt‘ye ekleyin.
String türündeki sayıyı int türüne çevirmek için buradaki yazıyı inceleyebilirsiniz.
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 | package com.example.myapplication; 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 { EditText editSayi1; EditText editSayi2; TextView textSonuc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editSayi1= findViewById(R.id.editSayi1); editSayi2= findViewById(R.id.editSayi2); textSonuc= findViewById(R.id.textSonuc); } private void btnHesapla(View v){ int sonuc = Integer.parseInt(editSayi1.getText().toString()) + Integer.parseInt(editSayi1.getText().toString()); textSonuc.setText(sonuc); } } |
Yorum yap