1

我正在尝试在首选项屏幕中使用 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>
4

2 回答 2

2

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

自定义首选项布局:

密码.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>

首选项屏幕布局:

首选项.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>

设置活动.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)
    }
}
于 2020-03-29T02:57:57.467 回答
0

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

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

android:dialogLayout="@layout/password"

代替:

android:layout="@layout/password"
于 2020-06-25T15:42:20.603 回答