परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके EditText के बाहर क्लिक करने पर या बटन दबाने पर सॉफ्ट कीबोर्ड को प्रोग्रामेटिकली कैसे छुपाया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML कॉन्फ़िगर करें
res/layout/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:padding="16dp"
tools:context="MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="यहां टेक्स्ट दर्ज करें"
android:textColor="@android:color/background_dark"
android:textSize="20sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="setText"
android:text="टेक्स्ट सेट करें"
android:textStyle="bold" />
</LinearLayout>
चरण 3: Kotlin कोड लागू करें
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var editText: EditText
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)
title = "KotlinApp"
}
fun setText(view: View) {
val newText = editText.text.toString()
textView.text = newText
closeKeyBoard()
}
private fun closeKeyBoard() {
val view = currentFocus
if (view != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
}
चरण 4: मैनिफेस्ट कॉन्फ़िगर करें
androidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<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>
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
अतिरिक्त: EditText के बाहर टच पर कीबोर्ड छुपाएं
पूरे लेआउट पर टच लिस्टनर जोड़कर EditText के बाहर टैप करने पर कीबोर्ड छुपाने के लिए:
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (ev?.action == MotionEvent.ACTION_DOWN) {
val view = currentFocus
if (view is EditText) {
val outRect = Rect()
view.getGlobalVisibleRect(outRect)
if (!outRect.contains(ev.rawX.toInt(), ev.rawY.toInt())) {
view.clearFocus()
closeKeyBoard()
}
}
}
return super.dispatchTouchEvent(ev)
}