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

Kotlin का उपयोग करके Android ListView में फोकसेबल EditText कैसे बनाएं?

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android ListView के अंदर फोकसेबल EditText कैसे बनाया जाता है। android:descendantFocusability="beforeDescendants" और listView.itemsCanFocus = true का उपयोग करके EditText को फोकस प्राप्त करने योग्य बनाया जाता है।

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

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

चरण 2: activity_main.xml में ListView कॉन्फ़िगर करें

<?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="4dp"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:descendantFocusability="beforeDescendants" />

</LinearLayout>

चरण 3: MainActivity.kt में कस्टम एडाप्टर बनाएं

import androidx.appcompat.app.AppCompatActivity
import android.annotation.SuppressLint
import android.app.LauncherActivity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.EditText
import android.widget.ListView
import java.util.ArrayList

class MainActivity : AppCompatActivity() {
    private lateinit var listView: ListView
    private lateinit var myAdapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        listView = findViewById(R.id.listView)
        listView.itemsCanFocus = true
        myAdapter = MyAdapter()
        listView.adapter = myAdapter
    }

    private inner class MyAdapter internal constructor() : BaseAdapter() {
        private var layoutInflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        internal var myItems = ArrayList<LauncherActivity.ListItem>()
        lateinit var context: Context

        init {
            for (i in 0..6) {
                val listItem = LauncherActivity.ListItem()
                listItem.className = "Caption$i"
                myItems.add(listItem)
            }
            notifyDataSetChanged()
        }

        override fun getItem(position: Int): Any = position
        override fun getItemId(position: Int): Long = position.toLong()
        override fun getCount(): Int = myItems.size

        @SuppressLint("InflateParams")
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
            var convertView = convertView
            val holder: ViewHolder
            if (convertView == null) {
                holder = ViewHolder()
                convertView = layoutInflater.inflate(R.layout.list, null)
                holder.caption = convertView.findViewById(R.id.ItemCaption)
                convertView.setTag(holder)
            } else {
                holder = convertView.tag as ViewHolder
            }
            holder.caption.setText(myItems.get(position).className)
            holder.caption.id = position
            holder.caption.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (!hasFocus) {
                    val position = v.id
                    val caption = v as EditText
                    myItems[position].className = caption.text.toString()
                }
            }
            return convertView
        }
    }

    internal inner class ViewHolder {
        lateinit var caption: EditText
    }
}

चरण 4: row लेआउट बनाएं (list.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingStart="4dp">

    <EditText
        android:id="@+id/ItemCaption"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dip"
        android:singleLine="true" />

</LinearLayout>

चरण 5: AndroidManifest.xml कॉन्फ़िगर करें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.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 डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।

मुख्य बिंदु:

  • descendantFocusability="beforeDescendants" - पैरेंट से पहले चाइल्ड को फोकस मिले
  • listView.itemsCanFocus = true - ListView आइटम्स को फोकस करने योग्य बनाता है
  • ViewHolder पैटर्न का उपयोग करके परफॉरमेंस ऑप्टिमाइज़ेशन
  • फोकस लॉस पर डेटा सेव करना
  1. - Kotlin का उपयोग करके Android पर स्क्रॉल करने योग्य TextView कैसे बनाएं?

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

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

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

  1. - Kotlin में Android SwipeRefreshLayout कैसे बनाएं?

    परिचय SwipeRefreshLayout उपयोगकर्ताओं को नीचे खींचकर कंटेंट रीफ्रेश करने की अनुमति देता है। यह आधुनिक एंड्रॉइड ऐप्स में एक सामान्य पैटर्न है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़ाइल में कोड जोड़ें res/layout/activity_main.xml में SwipeRefreshLayou