एंड्रॉइड में EditText इनपुट का पहला अक्षर स्वर/व्यंजन जांचना
यह ट्यूटोरियल दिखाता है कि उपयोगकर्ता इनपुट का पहला अक्षर हिंदी/अंग्रेजी स्वर (vowel) है या व्यंजन (consonant) कैसे पहचानें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:hint="कुछ टाइप करें"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonPanel"
android:text="जांचें"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity कोड (java/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editQuery = findViewById(R.id.edit_query);
findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = editQuery.getText().toString().trim();
if (!data.isEmpty()) {
char firstChar = Character.toLowerCase(data.charAt(0));
// अंग्रेजी स्वर जांचें
boolean isVowel = "aeiou".indexOf(firstChar) >= 0;
// हिंदी स्वर जांचें (देवनागरी यूनिकोड रेंज)
boolean isHindiVowel = isHindiVowel(firstChar);
if (isVowel || isHindiVowel) {
Toast.makeText(MainActivity.this, firstChar + " स्वर (Vowel) है", Toast.LENGTH_LONG).show();
} else if (Character.isLetter(firstChar)) {
Toast.makeText(MainActivity.this, firstChar + " व्यंजन (Consonant) है", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, firstChar + " अक्षर नहीं है", Toast.LENGTH_LONG).show();
}
} else {
editQuery.setError("कृपया टेक्स्ट दर्ज करें");
}
}
});
}
// हिंदी स्वर जांचने की सहायक विधि
private boolean isHindiVowel(char c) {
// देवनागरी स्वर: अ आ इ ई उ ऊ ऋ ए ऐ ओ औ (U+0905 से U+0914)
return (c >= '\u0905' && c <= '\u0914') ||
// मात्रा चिह्न भी शामिल करें यदि आवश्यक
(c >= '\u093E' && c <= '\u094C');
}
}
सुधार और विशेषताएं
- केस-इनसेंसिटिव जांच (
toLowerCase()) - हिंदी/देवनागरी स्वर सपोर्ट
Character.isLetter()से केवल अक्षर स्वीकार करें- संख्या/विशेष चिह्न के लिए अलग संदेश
- ट्रिमिंग से व्हाइटस्पेस समस्या हल
अतिरिक्त: स्ट्रिंग एक्सटेंशन फंक्शन (Kotlin)
fun Char.isVowel(): Boolean = "aeiouAEIOU".contains(this)
fun Char.isHindiVowel(): Boolean = this in '\u0905'..'\u0914' || this in '\u093E'..'\u094C'
// उपयोग
val firstChar = input.trim().firstOrNull()
if (firstChar != null) {
when {
firstChar.isVowel() || firstChar.isHindiVowel() -> "स्वर"
firstChar.isLetter() -> "व्यंजन"
else -> "अक्षर नहीं"
}
}
ऐप टेस्ट करें
टेक्स्ट दर्ज करें, जांचें दबाएं - पहला अक्षर स्वर/व्यंजन/अन्य बताएगा।