android - 创建单选风格首选项
问题描述
我正在尝试创建一个 Radio 样式Preference
,用户可以在其中单击一个选项,然后将其分配为的新值Preference
:
但是,当我单击一个选项时,会选择所有 3 个选项:
这是我的布局:
首选项.xml
<Preference
android:layout="@layout/preference_gender"
android:key="seeking"
android:title="Seeking"/>
偏好性别.xml
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/seeking"
android:singleLine="true"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorIcons"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/male"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/male"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/female"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textSize="@dimen/settings_textSize"
android:text="Male"
android:gravity="center"
android:background="@drawable/settings_gender"
android:padding="@dimen/settings_box_selection_padding"
android:paddingStart="@dimen/button_horizontal_padding"
android:paddingEnd="@dimen/button_horizontal_padding"
android:textColor="@color/colorPrimaryDark"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/female"
app:layout_constraintStart_toEndOf="@+id/male"
app:layout_constraintEnd_toStartOf="@id/both"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textSize="@dimen/settings_textSize"
android:text="Female"
android:gravity="center"
android:padding="@dimen/settings_box_selection_padding"
android:background="@drawable/settings_gender"
android:paddingStart="@dimen/button_horizontal_padding"
android:paddingEnd="@dimen/button_horizontal_padding"
android:textColor="@color/colorPrimaryDark"/>
<androidx.appcompat.widget.AppCompatTextView
app:layout_constraintStart_toEndOf="@id/female"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/both"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textSize="@dimen/settings_textSize"
android:text="Both"
android:gravity="center"
android:padding="@dimen/settings_box_selection_padding"
android:background="@drawable/settings_gender"
android:paddingStart="@dimen/button_horizontal_padding"
android:paddingEnd="@dimen/button_horizontal_padding"
android:textColor="@color/colorPrimaryDark"/>
</androidx.constraintlayout.widget.ConstraintLayout>
所以我希望能够单击一个文本视图,然后在Preference.OnPreferenceClickListener
.
知道怎么做吗?
编辑:
class PreferencesFragment : PreferenceFragmentCompat(), View.OnClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val genderPreference: Preference = findPreference("seeking")!!
val parentView = listView.getChildAt(genderPreference.order)
val childView = parentView.findViewById<TextView>(R.id.male) // NullPointerException
}
override fun onClick(v: View?) {
Log.d(TAG, "clicked") // doesn't log
when (v?.id){
R.id.male -> Log.d(TAG, "male")
}
}
}
解决方案
这可能是因为所有三个按钮都在一个首选项下,当您设置 时OnPreferenceClickListener
,它会选择整个首选项以及您作为一个整体包含的布局。
因此,OnPreferenceClickListener
您应该尝试获取父视图并在这些TextView
s 上单独设置点击侦听器,然后从那里更新首选项的值,而不是设置一个。
或者您可以将布局 xml 文件中onClick
的每个属性设置TextView
为所需的功能。
要获得此首选项的视图,您可以使用类似
View preferenceView = getListView().getChildAt(preference.getOrder());
或者,使用 getView 方法并为这两个参数提供 null:
View preferenceView = preference.getView(null, null);
如果您使用 AndroidX 库作为偏好,则没有getView
方法,因此唯一的选择是创建自定义 Preference 类并处理那里的视图:
public class CustomPreference extends Preference {
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onBindView(View rootView) {
super.onBindView(rootView);
View myView = rootView.findViewById(R.id.myViewId);
// get your textViews from myView
TextView maleText = myView.findViewById(R.id.male);
...
}
}
然后在你的preference.xml中使用它,如下所示:
<your.package.CustomPreference
android:key="your_key"
android:layout="@layout/your_layout" />
推荐阅读
- python - 如何遍历这本字典并访问这些项目?
- java - 如何使用 GeoJSON 属性值设置 Mapbox 符号图层文本字段?
- node.js - 将 nodejs 从 0.10 升级到 10.15.1 时出现 csrf() 403 禁止错误
- java - 如何在app中使用当前时间根据当前时间做出一个动作(从数据库中删除数据)
- c++ - 黑客等级极小极大问题,最大和给出错误的负值
- docker - 为什么 docker 的卷主机文件夹为空
- mongodb - 如何使用 MongoDB .NET 驱动程序查询索引访问计数?
- javascript - Javascript:用于包含多种类型的列表中的列表项的流类型
- c# - 如何将条目添加到 IIS 虚拟目录的配置
- xml - 使用 XPath 更新 Android Manifest 中的 versionCode