[英]difference between checkbox and checkedtextview in Android? Which to use for RecyclerView?
I am a beginner in Android Development.我是 Android 开发的初学者。 When I learned about CheckBox, it's a widget extending compoundbutton and CheckedTextView, widget extending TextView and implements Checkable Interface.
当我了解 CheckBox 时,它是一个扩展复合按钮和 CheckedTextView 的小部件,扩展 TextView 并实现 Checkable 接口的小部件。 When I searched on google, I found no results.
当我在谷歌上搜索时,我没有找到任何结果。 Actually, what is the difference between them, If I am using ListView or RecyclerView with CheckBox ability.
实际上,如果我使用具有 CheckBox 功能的 ListView 或 RecyclerView,它们之间有什么区别。 Which is the better option CheckBox or CheckedTextView?
CheckBox 或 CheckedTextView 哪个更好?
To supplement the other answers, here is a visual comparison between CheckBox and CheckedTextView.为了补充其他答案,这里是 CheckBox 和 CheckedTextView 之间的视觉比较。 This is running in an emulated Pixel 3 under Android R.
这是在 Android R 下的模拟 Pixel 3 中运行的。 Each of these screenshots shows three views inside a vertical LinearLayout.
这些屏幕截图中的每一个都显示了垂直线性布局内的三个视图。
If you don't specify the checkMark attribute, you get no checkbox on the CheckedTextView:如果不指定 checkMark 属性,则 CheckedTextView 上没有复选框:
with android:checkMark="?android:attr/listChoiceIndicatorSingle"
:与
android:checkMark="?android:attr/listChoiceIndicatorSingle"
:
with android:checkMark="?android:attr/listChoiceIndicatorMultiple"
:与
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
:
There are other differences, but they're covered by other answers.还有其他差异,但其他答案涵盖了它们。
As recommended from @LarsH I've turned my comment into an answer instead.根据@LarsH 的建议,我已将我的评论变成了答案。
The differences is CheckedTextView
doesn't have a checked/click event, see here why不同之处在于
CheckedTextView
没有选中/单击事件,请参阅此处为什么
Therefore if you want to have checked/click event for free (you can customize the checked/click listener by yourself) choose CheckBox
and its alignment by default is (left aligned TextView
& right aligned CheckBox
)因此,如果您想免费获得选中/单击事件(您可以自己自定义选中/单击监听器),请选择
CheckBox
,默认情况下其 alignment 为(左对齐TextView
和右对齐CheckBox
)
You might want to align the TextView
's text position (optional: if you want to align into different position, eg: right aligned TextView
& left aligned CheckBox
) You might want to align the
TextView
's text position (optional: if you want to align into different position, eg: right aligned TextView
& left aligned CheckBox
)
Then use it on your RecyclerView
holder然后在您的
RecyclerView
支架上使用它
CheckBox : Is a specific type of 2 states button that can be either checked or unchecked. CheckBox :是一种特定类型的 2 状态按钮,可以选中或取消选中。
CheckedTextView : Is a TextView with 2 states check and unchecked. CheckedTextView :是一个 TextView 有 2 个状态检查和未检查。
The text is the different between both views, so depend in what you want to do.两种视图之间的文本不同,因此取决于您要做什么。
About the second question, RecyclerView
was created as a ListView
improvement.关于第二个问题,
RecyclerView
是作为ListView
的改进而创建的。
so to set item in recyclerview first define the itemLayout as below所以要在recyclerview中设置项目首先定义itemLayout如下
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_checklist_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"
android:padding="5dp"
android:gravity="center"
/>
now in recyclerview you can use the checbox properties such as现在在 recyclerview 中,您可以使用复选框属性,例如
holder.checkBox.setText("position"+position);
to set the text of respective checkbox.设置各个复选框的文本。
you can also use other properties of checkbox such as您还可以使用复选框的其他属性,例如
holder.checkBox.setOnCheckedChangeListener();
and recyclerview is improved version of listview so try to use recyclerview. recyclerview 是 listview 的改进版本,所以尽量使用 recyclerview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.