当前位置:   article > 正文

Android 自定义单选按钮 RadioButton_android 自定义radiobutton

android 自定义radiobutton

一个简单自定义单选控件,传入图片资源即可

上代码:

  1. package com.example.SecondProject.View;
  2. import android.content.Context;
  3. import android.graphics.ColorMatrix;
  4. import android.graphics.ColorMatrixColorFilter;
  5. import android.util.AttributeSet;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.ImageView;
  9. import android.widget.LinearLayout;
  10. import androidx.annotation.Nullable;
  11. // 单选组:LinearLayout 传入图片id
  12. public class RadioGroupLinearLayout extends LinearLayout {
  13. private String TAG = "RadioGroupLinearLayout";
  14. private ImageView lastCheckedIcon; // 最后选中的icon
  15. private int[] iconResIds; // 选项图标资源
  16. private int iconWidth = 80;
  17. private int iconHeight = 80;
  18. public RadioGroupLinearLayout(Context context) {
  19. super(context);
  20. init();
  21. }
  22. public RadioGroupLinearLayout(Context context, @Nullable AttributeSet attrs) {
  23. super(context, attrs);
  24. init();
  25. }
  26. public RadioGroupLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  27. super(context, attrs, defStyleAttr);
  28. init();
  29. }
  30. private void init() {
  31. setOrientation(HORIZONTAL);
  32. }
  33. public void setIcons(int[] iconResIds,int default_index,int iconWidth,int iconHeight) {
  34. this.iconResIds = iconResIds; // 选项图标资源
  35. setIconsSize(iconWidth,iconHeight); // 设置选项图标大小
  36. createIconRadioButtons(default_index); // 初始化选项
  37. }
  38. // 设置选项图标大小
  39. public void setIconsSize(int iconWidth,int iconHeight){
  40. this.iconWidth = dp2px(getContext(),iconWidth);
  41. this.iconHeight = dp2px(getContext(),iconHeight);
  42. }
  43. // dp转px
  44. public static int dp2px(Context context, float dp) {
  45. float scale = context.getResources().getDisplayMetrics().density;
  46. return (int) (dp * scale + 0.5F);
  47. }
  48. // 初始化选项:传入默认选项的索引
  49. private void createIconRadioButtons(int default_index) {
  50. removeAllViews();
  51. for (int i = 0; i < iconResIds.length; i++) {
  52. final ImageView icon = new ImageView(getContext());
  53. // LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // 设置图标的大小为自适应
  54. LayoutParams params = new LayoutParams(iconWidth,iconHeight); // 设置图标的大小
  55. params.weight = 1;
  56. icon.setLayoutParams(params);
  57. icon.setImageResource(iconResIds[i]);
  58. setGrayColorFilter(icon);
  59. int finalI = i;
  60. icon.setOnClickListener(new OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. if (lastCheckedIcon==icon){return;} // 仍然是上次选中的
  64. setGrayColorFilter(lastCheckedIcon); // 点击新的图标时把上一个图片褪色
  65. lastCheckedIcon = icon;
  66. lastCheckedIcon.clearColorFilter(); // 清除褪色效果
  67. Log.e(TAG, "变身" );
  68. if (onItemClickListener!=null){onItemClickListener.onItemClick(finalI);}
  69. }
  70. });
  71. addView(icon); // 添加图标
  72. // 设置默认选项
  73. if (i == default_index) {
  74. lastCheckedIcon = icon;
  75. lastCheckedIcon.clearColorFilter();
  76. if (onItemClickListener!=null){onItemClickListener.onItemClick(i);}
  77. }
  78. }
  79. }
  80. // 设置褪色效果
  81. private void setGrayColorFilter(ImageView icon) {
  82. if(icon==null){return;}
  83. ColorMatrix colorMatrix = new ColorMatrix();
  84. colorMatrix.setSaturation(0);
  85. ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
  86. icon.setColorFilter(colorFilter);
  87. }
  88. // 接口 ----------------------------------------------------
  89. public interface onItemClickListener{
  90. void onItemClick(int index);
  91. }
  92. public onItemClickListener onItemClickListener;
  93. public void setOnItemClickListener(onItemClickListener onItemClickListener){this.onItemClickListener = onItemClickListener;}
  94. }

使用方法:

代码:

  1. package com.example.SecondProject
  2. import android.os.Bundle
  3. import android.util.Log
  4. import androidx.appcompat.app.AppCompatActivity
  5. import com.example.SecondProject.View.RadioGroupLinearLayout
  6. import com.example.SecondProject.databinding.ActivityRadioGroupBinding
  7. class RadioGroupActivity: AppCompatActivity() {
  8. val TAG = "RadioGroupActivity"
  9. lateinit var binding:ActivityRadioGroupBinding
  10. override fun onCreate(savedInstanceState: Bundle?) {
  11. super.onCreate(savedInstanceState)
  12. binding = ActivityRadioGroupBinding.inflate(layoutInflater)
  13. setContentView(binding.root)
  14. // 创建资源文件
  15. val iconResIds = intArrayOf(
  16. R.drawable.chibi_maruko,
  17. R.drawable.chibi_maruko,
  18. R.drawable.chibi_maruko,
  19. R.drawable.chibi_maruko,
  20. R.drawable.chibi_maruko,
  21. R.drawable.chibi_maruko,
  22. R.drawable.chibi_maruko,
  23. R.drawable.chibi_maruko,
  24. R.drawable.chibi_maruko
  25. )
  26. binding.radioGroup.setIcons(iconResIds,4,30,30) // 设置默认选项和图标大小
  27. // 设置点击事件
  28. binding.radioGroup.setOnItemClickListener(object :RadioGroupLinearLayout.onItemClickListener{
  29. override fun onItemClick(index: Int) {
  30. Log.e(TAG, "当前选中项: $index" )
  31. }
  32. })
  33. }
  34. }

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:gravity="center_vertical">
  7. <com.example.SecondProject.View.RadioGroupLinearLayout
  8. android:id="@+id/radio_group"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"/>
  11. </LinearLayout>

点击效果:



总体思路:

总体思路很简单:自定义一个 LinearLayout 设置为横向布局,并将传入的图片资源文件id逐个按照比重为1添加进去。定义一个 ImageView 对象作为最后选中的对象,当点击新的选项时把最后选中的对象去色并将自身定义为最后选中的对象再恢复颜色同时把点击的索引传出去。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/675865
推荐阅读
相关标签
  

闽ICP备14008679号