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

Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें?

Android में ButterKnife से व्यू बाइंडिंग सरल बनाना

ButterKnife एक व्यू बाइंडिंग लाइब्रेरी है जो findViewById() कॉल्स को एनोटेशन से बदल देती है। नोट: ButterKnife अब डिप्रिकेटेड है; आधुनिक ऐप्स के लिए View Binding (बिल्ट-इन) या Data Binding उपयोग करें।

⚠ डिप्रिकेटेड: Jake Wharton ने ButterKnife को मेंटेनेंस मोड में डाल दिया है। नए प्रोजेक्ट्स के लिए View Binding उपयोग करें जो कंपाइल-टाइम सेफ्टी देता है और किसी एनोटेशन प्रोसेसर की जरूरत नहीं।

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

Android Studio में File → New Project चुनें।

चरण 2: build.gradle (Module: app) में निर्भरता जोड़ें

dependencies {
    implementation 'com.jakewharton:butterknife:10.2.3'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
    // या Kotlin के लिए:
    // kapt 'com.jakewharton:butterknife-compiler:10.2.3'
}

चरण 3: लेआउट फ़ाइल (res/layout/activity_main.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:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="24dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="..........."
        android:textSize="18sp"
        android:textStyle="bold"
        android:textAllCaps="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="..........."
        android:textSize="18sp"
        android:textStyle="bold"
        android:textAllCaps="true" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:text="..........."
        android:textSize="18sp"
        android:textStyle="bold"
        android:textAllCaps="true" />

    <Button
        android:id="@+id/btnLoadText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="टेक्स्ट लोड करें"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

चरण 4: MainActivity (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    // @BindView एनोटेशन से व्यू ऑटोमैटिकली बाइंड होते हैं
    @BindView(R.id.textView1) TextView textView1;
    @BindView(R.id.textView2) TextView textView2;
    @BindView(R.id.textView3) TextView textView3;
    @BindView(R.id.btnLoadText) Button btnLoadText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // ButterKnife बाइंडिंग - findViewById कॉल्स की जरूरत नहीं
        ButterKnife.bind(this);
        
        // नोट: मूल कोड में बाइंडिंग के बाद फिर से findViewById किया गया था, जो अनावश्यक है
    }

    // @OnClick एनोटेशन से क्लिक लिसनर ऑटोमैटिक
    @OnClick(R.id.btnLoadText)
    public void onLoadTextClick(View view) {
        textView1.setText("नमस्ते, कैसे हैं आप?");
        textView2.setText("आपका दिन शुभ हो!");
        textView3.setText("आप बहुत अच्छे हैं।");
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // ButterKnife अनबाइंड (आमतौर पर एक्टिविटी में जरूरी नहीं, फ्रैगमेंट में जरूरी)
        // ButterKnife.unbind(this);
    }
}

ButterKnife मुख्य एनोटेशन्स

एनोटेशन उपयोग
@BindView व्यू बाइंड करने के लिए
@BindViews एकाधिक व्यूज को List में बाइंड
@OnClick क्लिक लिसनर
@OnLongClick लॉन्ग क्लिक लिसनर
@OnTextChanged TextWatcher शॉर्टकट
@BindString स्ट्रिंग रिसोर्स बाइंड
@BindColor कलर रिसोर्स बाइंड
@BindDimen डाइमेंशन रिसोर्स बाइंड

चरण 5: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <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>
                
                
            </intent-filter>
        </activity>

    </application>
</manifest>

आधुनिक विकल्प: View Binding (सुझाया गया)

build.gradle में सक्षम करें:

android {
    ...
    viewBinding {
        enabled = true
    }
}

फिर उपयोग करें:

// Activity में
private ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    
    binding.textView1.setText("नमस्ते!");
    binding.btnLoadText.setOnClickListener(v -> { ... });
}

ऐप चलाएं

ऐप लॉन्च करें, "टेक्स्ट लोड करें" बटन दबाएं, और तीनों TextView अपडेट होते देखें।

Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें?

Android में ButterKnife लाइब्रेरी का उपयोग कैसे करें?

  1. Android TextView में lastIndexOf() का उपयोग कैसे करें? Android TextView में lastIndexOf() का उपयोग कैसे करें?

    Android TextView में lastIndexOf() का उपयोग यह उदाहरण दर्शाता है कि Android TextView में lastIndexOf() मेथड का उपयोग करके किसी सबस्ट्रिंग का अंतिम इंडेक्स कैसे ढूंढा जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2

  1. Android में स्ट्रिंग तुलना का उपयोग कैसे करें? Android में स्ट्रिंग तुलना का उपयोग कैसे करें?

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

  1. Android में StringBuffer का उपयोग कैसे करें? Android में StringBuffer का उपयोग कैसे करें?

    Android में StringBuffer क्लास का उपयोग उदाहरण में जाने से पहले, हमें पता होना चाहिए कि StringBuffer क्या है। StringBuffer क्लास का उपयोग म्यूटेबल स्ट्रिंग बनाने के लिए किया जाता है और यह थ्रेड-सेफ है। यह उदाहरण दर्शाता है कि Android में StringBuffer का उपयोग कैसे करें। चरण 1: नया प्रोजेक्ट बनाएं A