【问题标题】:Android spinner adapter setDropDownViewResource custom layout with radiobutton带有单选按钮的 Android 微调器适配器 setDropDownViewResource 自定义布局
【发布时间】:2012-10-08 03:52:59
【问题描述】:

我在对话模式下使用 Spinner。 我使用 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 为 Spinner 设置 SimpleCursorAdapter 这很好用。

现在我尝试传递我的自定义布局而不是 simple_spinner_dropdown_item,它也可以很好地工作。

但是有一个但是...它没有原始 simple_spinner_dropdown_item 的单选按钮。 是否可以在显示微调器对话框时选择的自定义 spinner_dropdown_item 内添加单选按钮?

【问题讨论】:

    标签: android android-widget android-ui


    【解决方案1】:

    是的,但你必须为 spinner 定义另一个类。看看this

    您还有一个选择来满足您的要求。那是Alert dialog

    看看这个Alert Dialog Window with radio buttons in AndroidHow to create custom and drop down type dialog and Dialog in android

    【讨论】:

      【解决方案2】:

      好吧,我找到了解决方案。 ListView(微调器对话框内的内容)将检查您的 View 是否为 Checkable 并调用 setChecked。由于 android.R.layout.simple_spinner_dropdown_item 是可检查的,它可以工作。 因此,对于我的自定义列表项,我创建了实现 Checkable 的 LinearLayout

      public class CheckableLinearLayout extends LinearLayout implements Checkable
      {
      private boolean _isChecked = false;
      
      public CheckableLinearLayout(Context context)
          {
          super(context);
          }
      
      public CheckableLinearLayout(Context context, AttributeSet attrs)
          {
          super(context, attrs);
          }
      
      @Override
      public void setChecked(boolean checked)
          {
          _isChecked = checked;
      
          for (int i = 0; i < getChildCount(); i++)
              {
              View child = getChildAt(i);
              if (child instanceof Checkable)
                  {
                  ((Checkable) child).setChecked(_isChecked);
                  }
              }
          }
      
      @Override
      public boolean isChecked()
          {
          return _isChecked;
          }
      
      @Override
      public void toggle()
          {
          _isChecked = !_isChecked;
          }
      
      }
      

      所以 ListView 调用 setChecked 并将其传播到子视图,我的 CheckBox / RadioButton 将被正确选中/取消选中。

      【讨论】:

        猜你喜欢
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多