Есть ли простой способ получить элемент управления при загрузке из XML-файла с помощью Anko в Android Studio 3.1.2? - PullRequest
0 голосов
/ 31 мая 2018

Я создаю макет настройки параметров и отображаю пользовательский интерфейс с помощью UIPreference.kt.

Я могу обработать кнопку btnClose просто import kotlinx.android.synthetic.main.layout_preference.*.

Теперь я надеюсь обработать CheckBoxPreference chAutoRestore , но макет загружениз xml \ mypreference.xml, как мне это сделать?

В настоящее время я должен использовать код val checkboxPref = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference

UIPreference.kt

import kotlinx.android.synthetic.main.layout_preference.*

class UIPreference : AppCompatPreferenceActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_preference)

        fragmentManager.beginTransaction().replace(R.id.content, MyPreferenceFragment()).commit()


         btnClose.setOnClickListener {
            finish()
        }


    }


    class MyPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.mypreference)

            val checkboxPref = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference

            checkboxPref.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
                logError( "Pref " + preference.key + " changed to " + newValue.toString())
                true
            }

        }
    }

}

layout_preference.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/ad_unit_id"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toTopOf="@+id/btnClose"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/adView"
        >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />

    </FrameLayout>


    <Button
        android:id="@+id/btnClose"
        style="@style/myTextMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="2dp"
        android:text="@string/BtnClose"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

mypreference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="AppPreference"
    android:summary="@string/PreferenceSummary"
    android:title="@string/PreferenceTitle" >

    <PreferenceCategory android:title="General">
        <EditTextPreference
            android:defaultValue="7000"
            android:inputType="number"
            android:key="WebServerPort"
            android:summary="My PreferencePortSummary"
            android:title="My Title"

            />
    </PreferenceCategory>


    <PreferenceCategory android:title="Auto Restore">
        <CheckBoxPreference
            android:id="@+id/chAutoRestore"
            android:key="@string/IsAutoRestore"
            android:summary="Auto restore every 15"
            android:title="Auto Restore" />

    </PreferenceCategory>


</PreferenceScreen>

Раймонду Артеага

Спасибо!

chAutoRestore является идентификатором CheckBoxPreference android:id="@+id/chAutoRestore"

Я надеюсь, что смогу использовать следующий код

chAutoRestore.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
             ...
}

Так же, как

btnClose - это идентификаторКнопка android:id="@+id/btnClose"

Я могу использовать код по import kotlinx.android.synthetic.main.layout_preference.*.

btnClose.setOnClickListener {
  ...
}
...