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

Android Studio में फ्रैगमेंट ट्यूटोरियल उदाहरण के साथ

Android में फ्रैगमेंट ट्यूटोरियल

यह उदाहरण Android Studio में फ्रैगमेंट ट्यूटोरियल को उदाहरण के साथ दर्शाता है।

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

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

चरण 2: res/layout/activity_main.xml में कोड जोड़ें

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <Button
        android:id="@+id/fragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:text="fragment1"/>
    <Button
        android:id="@+id/fragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:text="fragment2"/>
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

उपरोक्त कोड में, हमने अलग-अलग फ्रैगमेंट दिखाने के लिए बटन व्यूज और लीनियर लेआउट लिया है।

चरण 3: src/MainActivity.java में कोड जोड़ें

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final android.support.v4.app.Fragment first = new FirstFragment();
        final android.support.v4.app.Fragment second = new SecondFragment();

        findViewById(R.id.fragment1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.layout, first);
                fragmentTransaction.commit();
            }
        });

        findViewById(R.id.fragment2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.layout, second);
                fragmentTransaction.commit();
            }
        });
    }
}

चरण 4: src/FirstFragment.java में कोड जोड़ें

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class FirstFragment extends Fragment {
    TextView textView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        textView = view.findViewById(R.id.text);
        textView.setText("first");
        return view;
    }
}

चरण 5: src/SecondFragment.java में कोड जोड़ें

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment {
    TextView textView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        textView = view.findViewById(R.id.text);
        textView.setText("Second");
        return view;
    }
}

चरण 6: res/layout/fragment.xml में कोड जोड़ें

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

ऐप चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफॉल्ट स्क्रीन देखें।

Android Studio में फ्रैगमेंट ट्यूटोरियल उदाहरण के साथ

अब बटन्स पर क्लिक करें, यह नीचे दिखाए अनुसार परिणाम दिखाएगा:

Android Studio में फ्रैगमेंट ट्यूटोरियल उदाहरण के साथ

Android Studio में फ्रैगमेंट ट्यूटोरियल उदाहरण के साथ

  1. एंड्रॉइड में BroadcastReceiver के साथ नोटिफिकेशन कैसे बनाएँ? एंड्रॉइड में BroadcastReceiver के साथ नोटिफिकेशन कैसे बनाएँ?

    यह ट्यूटोरियल दर्शाता है कि **BroadcastReceiver** के साथ **AlarmManager** का उपयोग करके नोटिफिकेशन कैसे बनाए और शेड्यूल किए जाएँ। यह पैटर्न ऐप प्रोसेस न चलने पर भी सटीक समय-आधारित डिलीवरी सक्षम करता है। आर्किटेक्चर अवलोकन MainActivity → AlarmManager.setExact() → PendingIntent (Broadcast) → My

  1. jQuery closest() मेथड उदाहरण के साथ jQuery closest() मेथड उदाहरण के साथ

    jQuery में closest() मेथड का उपयोग चयनित तत्व के पहले पूर्वज (ancestor) को लौटाने के लिए किया जाता है। सिंटैक्स सिंटैक्स इस प्रकार है: $(selector).closest(filter) उदाहरण आइए अब jQuery closest() मेथड को लागू करने का एक उदाहरण देखें: <!DOCTYPE html> <html> <head> <style>

  1. Redis GEORADIUSBYMEMBER का उपयोग: उदाहरणों के साथ व्यावहारिक गाइड Redis GEORADIUSBYMEMBER का उपयोग: उदाहरणों के साथ व्यावहारिक गाइड

    GEORADIUSBYMEMBER कमांड यह कमांड की पर संग्रहीत जियोस्पेशियल मान (सॉर्टेड सेट) के एक या अधिक मेंबर्स को लौटाने के लिए उपयोग किया जाता है, जो निर्दिष्ट मेंबर के लॉन्गिट्यूड, लैटिट्यूड मान को सर्कल के केंद्र स्थान के रूप में और निर्दिष्ट यूनिट में रेडियस को सर्कल की त्रिज्या के रूप में उपयोग करके गणना