পূর্ব প্রস্তুতি
এই উদাহরণটি দেখায় কিভাবে বাটন ক্লিক করলে EditText-এর `inputType` প্রোগ্রামেটিক্যালি নামেরিক করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করুন।
ধাপ ২: 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"
android:padding="4dp">
<TextView
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="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerInParent="true"
android:onClick="setInputTypeNumber"
android:text="Click here" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt-এ ইনপুট টাইপ সেট করুন
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.InputType
import android.view.View
import android.widget.EditText
class MainActivity : AppCompatActivity() {
private lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
editText = findViewById(R.id.editText)
}
fun setInputTypeNumber(view: View) {
editText.inputType = InputType.TYPE_CLASS_NUMBER
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.inputtype">
<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>
পরীক্ষা করুন
অ্যাপ চালান। প্রথমে নরমাল কিবোর্ড আসবে। "Click here" বাটনে ক্লিক করলে কিবোর্ড নামেরিক হয়ে যাবে।
XML-এ সেট করার বিকল্প
XML-এ `android:inputType="number"` লিখলেই কোড লিখতে হয় না। প্রোগ্রামেটিক্যালি সেট করলে রানটাইমে ডাইনামিক্যালি পরিবর্তন করা যায়।