EditText-এ InputType ডাইনামিকভাবে পরিবর্তন
রানটাইমে Java/Kotlin কোড দিয়ে EditText-এর ইনপুট টাইপ (নাম্বার, টেক্সট, পাসওয়ার্ড, ইমেইল ইত্যাদি) পরিবর্তন করার পদ্ধতি।
ধাপ ১: প্রজেক্ট তৈরি
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
android:padding="4dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something..." />
<Button
android:id="@+id/btnNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Set Input Type: NUMBER" />
<Button
android:id="@+id/btnText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Set Input Type: TEXT" />
<Button
android:id="@+id/btnPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Set Input Type: PASSWORD" />
<Button
android:id="@+id/btnEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Set Input Type: EMAIL" />
</LinearLayout>
ধাপ ৩: MainActivity.java
import androidx.appcompat.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 {
EditText editText;
Button btnNumber, btnText, btnPassword, btnEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnNumber = findViewById(R.id.btnNumber);
btnText = findViewById(R.id.btnText);
btnPassword = findViewById(R.id.btnPassword);
btnEmail = findViewById(R.id.btnEmail);
btnNumber.setOnClickListener(v ->
editText.setInputType(InputType.TYPE_CLASS_NUMBER));
btnText.setOnClickListener(v ->
editText.setInputType(InputType.TYPE_CLASS_TEXT));
btnPassword.setOnClickListener(v ->
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
btnEmail.setOnClickListener(v ->
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS));
// কার্সর শেষে সরানোর জন্য
editText.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) editText.setSelection(editText.getText().length());
});
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.inputtype">
<application ...>
<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>
InputType কনস্ট্যান্টস রেফারেন্স
| কনস্ট্যান্ট | ব্যাখ্যা | কীবোর্ড টাইপ |
|---|---|---|
TYPE_CLASS_TEXT | সাধারণ টেক্সট | QWERTY |
TYPE_CLASS_NUMBER | সুইচ নম্বর | নাম্বার প্যাড |
TYPE_CLASS_PHONE | ফোন নম্বর | ডায়ালপ্যাড |
TYPE_CLASS_DATETIME | তারিখ/সময় | ডেট/টাইম পিকার |
TYPE_TEXT_VARIATION_PASSWORD | পাসওয়ার্ড (হাইড) | টেক্সট + হাইড |
TYPE_TEXT_VARIATION_EMAIL_ADDRESS | ইমেইল | QWERTY + @ .com |
TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | দেখানো পাসওয়ার্ড | টেক্সট + আনহাইড |
TYPE_TEXT_VARIATION_URI | URL | QWERTY + / .com |
TYPE_TEXT_FLAG_CAP_WORDS | প্রতিটি শব্দ ক্যাপিটাল | টেক্সট + অটো ক্যাপ |
TYPE_TEXT_FLAG_MULTI_LINE | মাল্টিলাইন | এন্টার কী |
কম্বিনেশন উদাহরণ
সেট করুন ডটগুলো সুন্দর দেখানোর জন্য