EditText ফিল্ড থেকে ডেটা নেওয়ার টিউটোরিয়াল
এই উদাহরণটি দেখায় কিভাবে কোটলিন ব্যবহার করে এন্ড্রয়েডে EditText ফিল্ড থেকে ব্যবহারকারীর ইনপুট মান পাওয়া যায় এবং সেটি TextView-এ প্রদর্শন করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML ফাইল কনফিগার করুন
res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button"
android:layout_centerInParent="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="মান পান" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_centerInParent="true"
android:layout_marginTop="8dp"
android:textColor="@android:color/background_dark"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt ফাইলে লজিক লিখুন
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var button: Button
lateinit var editText: EditText
lateinit var string: String
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
button = findViewById(R.id.button)
editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)
button.setOnClickListener {
string = editText.text.toString()
textView.text = string
}
}
}
ধাপ ৪: AndroidManifest.xml আপডেট করুন
সাধারণ ম্যানিফেস্ট কনফিগারেশন যথেষ্ট।
অ্যাপ্লিকেশন চালানোর পদ্ধতি
এন্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Run আইকনে ক্লিক করুন এবং ডিভাইস নির্বাচন করুন। EditText-এ কিছু লিখে "মান পান" বাটনে ক্লিক করলে নিচে TextView-তে লেখাটি দেখাবে।