Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके एंड्रॉइड के लिए सॉफ्ट कीबोर्ड कैसे विकसित करें?

परिचय

यह उदाहरण एक बेसिक एंड्रॉइड ऐप दर्शाता है जो EditText के साथ इंटरैक्ट करता है। ध्यान दें: एक पूर्ण कस्टम सॉफ्ट कीबोर्ड (IME) बनाने के लिए InputMethodService की आवश्यकता होती है, जो इस सरल उदाहरण के दायरे से बाहर है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: लेआउट XML कॉन्फ़िगर करें

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="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText"
        android:layout_centerInParent="true"
        android:layout_marginTop="5dp"
        android:onClick="showText"
        android:text="टेक्स्ट दिखाएं" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnShow"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/background_dark"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="सॉफ्ट कीबोर्ड"
        android:textColor="@android:color/holo_red_light"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

चरण 3: Kotlin कोड लागू करें

src/MainActivity.kt में निम्नलिखित कोड जोड़ें:

import android.os.Bundle
import android.view.View
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)
    }

    fun showText(view: View) {
        val newText = editText.text.toString()
        textView.text = newText
    }
}

चरण 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 के साथ बेसिक इंटरैक्शन दिखाता है। एक वास्तविक कस्टम सॉफ्ट कीबोर्ड (Input Method Editor - IME) बनाने के लिए:

  1. InputMethodService को एक्सटेंड करें
  2. कीबोर्ड लेआउट XML परिभाषित करें
  3. onCreateInputView() में कस्टम व्यू इनफ्लेट करें
  4. मैनिफेस्ट में <service> के रूप में घोषित करें जिसमें android.permission.BIND_INPUT_METHOD हो
  5. res/xml/method.xml में मेटाडेटा प्रदान करें

यह एक जटिल विषय है जिसके लिए अलग विस्तृत ट्यूटोरियल की आवश्यकता होती है।

  1. Android के लिए सॉफ्ट कीबोर्ड कैसे विकसित करें? Android के लिए सॉफ्ट कीबोर्ड कैसे विकसित करें?

    यह उदाहरण एक बेसिक Android ऐप दर्शाता है जो EditText इनपुट को TextView में दिखाता है - वास्तविक सॉफ्ट कीबोर्ड विकास के लिए InputMethodService की आवश्यकता होती है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: UI लेआउट res/layout/activity_main.xml: <?xml version=

  1. - Kotlin का उपयोग करके Android पर रनटाइम में TextView की स्टाइल कैसे बदलें?

    परिचय यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android पर रनटाइम में TextView की स्टाइल कैसे बदली जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio खोलें, File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: activity_main.xml लेआउट जोड़ें res/layout/activity_main.xml में

  1. - Kotlin का उपयोग करके एंड्रॉइड एमुलेटर का IP पता कैसे प्राप्त करें?

    परिचय यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड एमुलेटर या डिवाइस का वाईफाई IP पता प्रोग्रामेटिक रूप से कैसे प्राप्त करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट फ़ाइल में कोड जोड़ें res/layout/activity_main.xml: <?xml version="1.0"