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

Kotlin का उपयोग करके Android Spinner को प्रारंभिक डिफ़ॉल्ट टेक्स्ट के साथ कैसे बनाएं?

परिचय

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android Spinner को प्रारंभिक डिफ़ॉल्ट टेक्स्ट के साथ कैसे बनाया जाए।

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

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

चरण 2: activity_main.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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Spinner Sample Program"
        android:textColor="@android:color/background_dark"
        android:textSize="16sp" />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp">
    </Spinner>
</LinearLayout>

चरण 3: MainActivity.kt लागू करें

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

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    lateinit var spinner: Spinner
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        spinner = findViewById(R.id.spinner)
        
        val footballPlayers: MutableList<String?> = ArrayList()
        footballPlayers.add(0, "Select a player from the list")
        footballPlayers.add("Cristian Ronaldo")
        footballPlayers.add("Lionel Messi")
        footballPlayers.add("Neymar Jr")
        footballPlayers.add("Isco")
        footballPlayers.add("Gareth Bale")
        footballPlayers.add("Luis Suarez")
        
        val arrayAdapter: ArrayAdapter<String?> = ArrayAdapter(this, android.R.layout.simple_list_item_1, footballPlayers)
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = arrayAdapter
        
        spinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                if (parent.getItemAtPosition(position) == "Choose Football players from list") {
                    // डिफ़ॉल्ट आइटम चयनित - कुछ न करें
                } else {
                    val item = parent.getItemAtPosition(position).toString()
                    Toast.makeText(parent.context, "Selected: $item", Toast.LENGTH_SHORT).show()
                }
            }
            
            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }
    }
}

चरण 4: 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 आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें।

  1. Android में मार्की टेक्स्ट कैसे बनाएं? Android में मार्की टेक्स्ट कैसे बनाएं?

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

  1. Android में डिफ़ॉल्ट टेक्स्ट वाला स्पिनर (Spinner) कैसे बनाएं? Android में डिफ़ॉल्ट टेक्स्ट वाला स्पिनर (Spinner) कैसे बनाएं?

    Android में चुनें... जैसे डिफ़ॉल्ट टेक्स्ट वाला स्पिनर बनाने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे ArrayAdapter और स्पिनर में पहला आइटम डिफ़ॉल्ट प्लेसहोल्डर के रूप में उपयोग करके Select a player... जैसा प्रारंभिक टेक्स्ट दिखाया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project

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

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