এই উদাহরণটি প্রদর্শন করে কীভাবে কোটলিন ব্যবহার করে শুধু অক্ষর (alphabets) গ্রহণকারী EditText তৈরি করা যায়।
পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।
পদক্ষেপ ২: res/layout/activity_main.xml-এ কোড যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
tools:context=".MainActivity">
<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="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Enter text here" />
</RelativeLayout>
পদক্ষেপ ৩: src/MainActivity.kt-এ কোড যোগ করুন
import android.os.Bundle
import android.text.InputFilter
import android.text.Spanned
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val editText = findViewById<EditText>(R.id.editText)
// শুধু অক্ষর (A-Z, a-z) এবং স্পেস অনুমতি
val alphaFilter = InputFilter { source, start, end, _, _, _ ->
val input = source.subSequence(start, end).toString()
if (input.matches(Regex("^[a-zA-Z\\s]*$"))) {
null // গ্রহণযোগ্য
} else {
"" // ব্লক করুন
}
}
editText.filters = arrayOf(alphaFilter)
}
}
বিকল্প পদ্ধতি (XML-এ):
<EditText
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
... />
পদক্ষেপ ৪: androidManifest.xml-এ কোড যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন চালান
আপনার আসল অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে ধরে। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।