Assalomu alaykum. Bugun sizlar bilan birgalikda AndroidStudio-da PreferenceFragment bilan ishlashni ko'rib chiqamiz. Demak, boshladik.  

Sozlamalar bilan ishlashni osonlashtirish uchun Android fragmentning maxsus turi - PreferenceFragment-ni taqdim etadi. Keling, uni qanday ishlatishni ko'rib chiqaylik.

Demak, Android Studioda yangi ilova yaratib, avval res papkasiga xml papkasini qo'shing. Buning uchun res papkasiga kursorni olib borib, sichqonchaning o'ng tugmasini bosing va New -> Android Resource Directory -ni tanlang:

Keyin yangi ochilgan oynada, resurs turi va papka nomini xml deb qo'ying va OK tugmasini bosing:

xml papkasi qo'shildi.

Keyin res/xml papkasiga yangi fayl qo'shing va settings.xml deb nomlang. So'ngra, shu faylni quyidagicha o'zgartiring:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">;
    <EditTextPreference
        android:key="login"
        android:summary="Loginni kiriting"
        android:title="Login" />
    <CheckBoxPreference
        android:key="enabled"
        android:summary="Loginni ko'rsatish"
        android:title="Ko'rsatish" />
</PreferenceScreen>

Bu kodda EditTextPreference va CheckBoxPreference elementlari PreferenceScreen elementining ichida aniqlanadi. Ushbu elementlarning har biri orqali biz ma'lum bir sozlama bilan o'zaro aloqada bo'lishimiz mumkin.

Aslida, biz bir qator turli xil sozlamalardan foydalanishimiz mumkin:

  • EditTextPreference: Matn qiymatini kiritish uchun EditText elementidan foydalaniladi.
  • CheckBoxPreference: mantiqiy qiymatlarni, ya'ni, true yoki false-ni o'rnatish uchun CheckBox elementidan foydalaniladi.
  • SwitchPreference: mantiqiy qiymatlarni o'rnatish uchun Switch elementidan foydalanadi. (true yoki false) (on va off).
  • RingtonePreference: mantiqiy qiymatlarni o'rnatish uchun qo'ng'iroq ohanglari ro'yxatidan qo'ng'iroq ohangini o'rnatish uchun dialog oynasidan foydalanadi (true yoki false).
  • ListPreference: oldindan belgilangan qiymatlardan birini tanlash uchun ro'yxatdan foydalanadi.
  • MultiSelectListPreference: Qiymatlarni tanlash uchun ro'yxatdan foydalaniladi, lekin bir nechta elementni tanlashga imkon beradi.

Har bir sozlama elementi uchun siz kamida uchta atributni aniqlashingiz kerak:

  • android:key: sozlama kaliti SharedPreferences-ga o'rnatadi.
  • android:title: foydalanuvchi uchun sozlamaning sarlavhasi.
  • android:summary: foydalanuvchi uchun ushbu sozlamaning qisqacha tavsifi.

Va endi sozlamalarni o'rnatish uchun ilovaga maxsus activity-ni qo'shing. Buni SettingsActivity deb nomlang. Endi shu activity-ni quyidagicha o'zgartiring:

package com.example.android.preferencefragment;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        getFragmentManager().beginTransaction().add(R.id.prefs_content, new SettingsFragment())
                .commit();
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.settings);
        }
    }
}

SettingsActivity interfeys layout-i sifatida R.layout.activity_settings resursidan foydalanadi.

Ishga tushirilgandan so'ng, SettingsActivity fragment SettingsFragment-ni id-si prefs_content bo'lgan element bilan yuklaydi.

Fragment SettingsFragment PreferenceFragment sinfidan olinadi. Uning onCreate() metodida addPreferencesFromResource() metodi chaqiriladi, unga xml resursi uzatiladi (bu holatda, avval aniqlangan R.xml.settings resursi).

Endi res/layout papkasida quyidagi activity_settings.xml faylini aniqlaymiz va o'zgartiramiz:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/prefs_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Bu kodda FrameLayout id = prefs_content bilan aniqlangan - bu SettingsFragment fragmenti yuklanadigan element.

Natijada, project quyidagi ko'rinishga ega bo'ladi:

 

Keyin asosiy activity - MainActivity-ga o'tamiz. activity_main.xml fayliga TextView va Button-ni qo'shing:

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

    <TextView
        android:id="@+id/login_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="setPrefs"
        android:text="Sozlamalar" />

</LinearLayout>

MainActivity.java faylini o'zgartiring:

package com.example.android.preferencefragment;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView loginText;
    boolean enabled;
    String login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loginText = findViewById(R.id.login_text);
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        enabled = prefs.getBoolean("enabled", false);
        login = prefs.getString("login", "o'rnatilmagan");
        loginText.setText(login);
        if (enabled)
            loginText.setVisibility(View.VISIBLE);
        else
            loginText.setVisibility(View.INVISIBLE);
    }

    public void setPrefs(View view) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }
}

onResume() metodida biz barcha sozlamalarni olamiz. Agar enabled sozlamasi true bo'lsa, u holda TextViewda login ko'rsatiladi.

setPrefs() metodi tugma bosilganda aniqlanadi. Ya'ni, SettingsActivity ga o'tiladi.

Ilovani birinchi marta ishga tushirganingizda hech qanday sozlamalar bo'lmaydi va login ko'rsatilmaydi. Sozlamalar sahifasiga o'tasiz va u erda tizimga kirishni o'rnatib, uning ko'rinishini yoqasiz va keyin asosiy activity-ga qaytasiz.

Ilovani ishga tushiring va natijani ko'ring:

"Sozlamalar" tugmasini bosing:

Loginni kiriting, masalan, men shunchaki harflar ketma-ketligini kiritdim:


Endi loginni ekranda ko'rsatish uchun "Ko'rsatish" ni yoqib qo'ying:

Va asosiy ekranga qaytib, natijani ko'ring:

 

Barcha sozlamalar avtomatik ravishda PreferenceFragment yordamida saqlanadi.

 

Sizlarga ulashmoqchi bo'lgan ma'lumotlarim shulardan iborat edi. Sizlarga foydali bo'ldi degan umiddaman. 

 

"ONE MILLION UZBEK CODERS" BILAN YANGI MARRALARNI ZABT ETING!