यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में EditText पर ईमेल पते को कैसे मान्य किया जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में: File → New Project चुनें और आवश्यक विवरण भरें।
चरण 2 - activity_main.xml लेआउट जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/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"
android:layout_margin="20dp"
android:hint="ईमेल आईडी दर्ज करें" />
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerInParent="true"
android:text="मान्यता जांचें"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp" />
</RelativeLayout>
चरण 3 - MainActivity.kt में मान्यता लॉजिक जोड़ें
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var btnCheck: Button
private lateinit var editText: EditText
private val emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
btnCheck = findViewById(R.id.btnCheck)
editText = findViewById(R.id.editText)
btnCheck.setOnClickListener {
val input = editText.text.toString().trim()
if (input.isEmpty()) {
Toast.makeText(this, "कृपया ईमेल पता दर्ज करें", Toast.LENGTH_SHORT).show()
} else if (input.matches(emailPattern.toRegex())) {
Toast.makeText(this, "मान्य ईमेल पता", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "अमान्य ईमेल पता", Toast.LENGTH_SHORT).show()
}
}
}
}
चरण 4 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.emailvalidation">
<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>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। ईमेल दर्ज करें और "मान्यता जांचें" बटन दबाएं। सही फॉर्मेट (उदाहरण: user@example.com) के लिए "मान्य ईमेल पता" दिखेगा, अन्यथा "अमान्य ईमेल पता"।