在Android Studio 3.1.2中使用Anko从xml文件加载时是否有一种简单的方法来获取控件?

时间:2018-05-31 02:05:37

标签: android anko

我创建了一个自定义Preference布局,并使用UIPreference.kt显示UI。

我可以通过import kotlinx.android.synthetic.main.layout_preference.*.

轻松处理按钮 btnClose

现在我希望简单地处理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>

致Raymond Arteaga

谢谢!

chAutoRestore是CheckBoxPreference android:id="@+id/chAutoRestore"

的id

我希望我可以使用以下代码

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

就像

一样

btnClose是Button android:id="@+id/btnClose"

的id

我可以使用import kotlinx.android.synthetic.main.layout_preference.*.

之前的代码
btnClose.setOnClickListener {
  ...
}

0 个答案:

没有答案