EditText-কে নিউমেরিক ইনপুট-এ সীমাবদ্ধ করুন
এই সহজ টিউটোরিয়ালে দেখানো হয়েছে কিভাবে বাটন ক্লিক বা XML-এ EditText-কে শুধু সংখ্যা গ্রহণ করতে বাধ্য করা যায়।
ধাপ ১: প্রজেক্ট তৈরি
ধাপ ২: লেআউট
<?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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Click Here to set numeric value for EditText"/>
</RelativeLayout>
ধাপ ৩: MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(v -> {
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
});
}
}
XML-এ সরাসরি সেট করা (সেরা পদ্ধতি)
জাভা কোড ছাড়াই XML-এ সেট করাই ভালো:
<EditText
android:inputType="number"
android:digits="0123456789" />
InputType এর ধরনসমূহ
| InputType | বিবরণ |
|---|---|
| TYPE_CLASS_NUMBER | সাধারণ সংখ্যা (সাইন্ড ইন্টিজার) |
| TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL | দশমিক সংখ্যা |
| TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED | নেগেটিভ সংখ্যা অনুমোদন |
| TYPE_CLASS_PHONE | ফোন নম্বর কيبোর্ড |
| TYPE_CLASS_DATETIME | তারিখ/সময় ইনপুট |
কীবোর্ড কাস্টমাইজেশন
<EditText
android:inputType="numberDecimal"
android:digits="0123456789."
android:maxLength="10" />
দশমিক স্থান সীমাবদ্ধ করা
আগের আর্টিকেলে (১০০৬০৬১৬৪৯) দেখানো হয়েছে InputFilter ব্যবহার করে দশমিক স্থান সীমাবদ্ধ করা।