【问题标题】:How to use TextInputLayout in preferenceScreen如何在preferenceScreen中使用TextInputLayout
【发布时间】:2020-03-22 14:47:07
【问题描述】:

我正在尝试在首选项屏幕中使用 TextInputLayout,以便拥有一个漂亮整洁的密码框,其中包含显示密码选项。我知道有一些方法和方法可以使用复选框和脚本来做到这一点,但我真的很想只使用 TextInputLayout,可能是包装的或自定义的小部件。 Gradle 依赖项是正确的,因为它在其他活动屏幕中运行良好。

以下preferences.xml 因“错误膨胀类com.google.android.material.textfield.TextInputLayout”而崩溃

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

</androidx.preference.PreferenceScreen>

【问题讨论】:

    标签: android kotlin preferencescreen textinputlayout


    【解决方案1】:

    好的,我想出了答案(并在此过程中学到了很多东西)。进行了大量研究和反复试验,但我希望以下内容可能对同样沮丧的人有所帮助。

    自定义首选项布局:

    password.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textLay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="66dp"
        android:hint="@string/Set_Password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:passwordToggleEnabled="true">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@android:id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />
    
    </com.google.android.material.textfield.TextInputLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    偏好屏幕布局:

    preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <com.toggen.myapplication.PasswordPreference
        android:key="pref_password"
        android:layout="@layout/password" />
    
    </PreferenceScreen>
    

    SettingsActivity.kt

    package com.toggen.myapplication
    
    import kotlinx.android.synthetic.main.password0.view.*
    import androidx.appcompat.app.AppCompatActivity
    import android.util.AttributeSet
    import android.widget.EditText
    import android.content.Context
    import androidx.preference.*
    import android.os.Bundle
    
    class PasswordPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs){
        private var passW: EditText? = null
    
        override fun onBindViewHolder(holder: PreferenceViewHolder?) {
            super.onBindViewHolder(holder)
            passW = holder?.itemView?.textLay?.editText?.apply{ setText(getPersistedString("admin")) }
        }
    
        override fun onDetached() {
            super.onDetached()
            passW?.apply{ persistString("$text") }
        }
    }
    
    class SettingsActivity:  AppCompatActivity()  {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            if(savedInstanceState == null) {
                supportActionBar?.title = "Settings"
                supportFragmentManager.beginTransaction()
                    .replace(android.R.id.content, SettingsFragment()).commit()
            }
        }
        override fun onSupportNavigateUp() = onBackPressed().run { true }
    
        class SettingsFragment : PreferenceFragmentCompat() {
            override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) = setPreferencesFromResource(R.xml.preferences, rootKey)
        }
    }
    

    【讨论】:

      【解决方案2】:

      终于找到了我一直在寻找的答案!!谢谢!!

      我要做的唯一修改是使用:

      android:dialogLayout="@layout/password"
      

      代替:

      android:layout="@layout/password"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        • 2021-08-29
        • 2021-01-24
        • 1970-01-01
        • 2015-08-14
        • 2015-12-26
        • 1970-01-01
        相关资源
        最近更新 更多