การจำกัด EditText ให้รับเฉพาะตัวเลข (Numeric Input)
ตัวอย่างนี้สาธิตวิธีตั้งค่า EditText ให้รับค่าตัวเลขอย่างเดียว โดยใช้ InputType.TYPE_CLASS_NUMBER ทั้งใน XML และโค้ด Java
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: จัดทำ Layout (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"
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="ตั้งค่าเป็นตัวเลข" />
</RelativeLayout>
วิธีที่ 1: ตั้งค่าใน XML (แนะนำ - เรียบง่ายที่สุด)
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10" />
วิธีที่ 2: ตั้งค่าในโค้ด 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(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ตั้งค่าให้รับเฉพาะตัวเลข
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
}
});
}
}
ขั้นตอนที่ 3: กำหนดค่าใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
การทดสอบ
รันแอปพลิเคชัน กดปุ่ม "ตั้งค่าเป็นตัวเลข" แล้วลองพิมพ์ใน EditText จะพิมพ์ได้เฉพาะตัวเลข 0-9



ตัวเลือก InputType สำหรับตัวเลข
| InputType | คำอธิบาย | คีย์บอร์ดที่แสดง |
|---|---|---|
TYPE_CLASS_NUMBER |
ตัวเลขจำนวนเต็ม (0-9) | นัมเบอร์แพด |
TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL |
ตัวเลขทศนิยม (มีจุดทศนิยม) | นัมเบอร์แพด + จุด |
TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED |
ตัวเลขมีเครื่องหมาย (+/-) | นัมเบอร์แพด + เครื่องหมาย |
TYPE_CLASS_PHONE |
เบอร์โทรศัพท์ | โทรศัพท์แพด (+, *, #) |
ใน XML ใช้ค่า string แทน
android:inputType="number" <!-- จำนวนเต็ม --> android:inputType="numberDecimal" <!-- ทศนิยม --> android:inputType="numberSigned" <!-- มีเครื่องหมาย --> android:inputType="phone" <!-- เบอร์โทร -->