【问题标题】:Setting a View disabled but keeping the color设置视图禁用但保持颜色
【发布时间】:2013-06-14 21:00:43
【问题描述】:

我有一个与 BaseExpandableListAdapter 关联的 ExpendableListView。在这个列表中,当用户点击一个孩子时,它会触发一些方法并将一个复选框设置为选中(如果用户再次点击,同样的事情,但复选框未选中)。

为了能够正确使用 onChildClick 方法,我已将复选框设置为 android:enabled="false",正如许多帖子所建议的那样。

问题是,复选框自动变为灰色,无论是否选中。它不会影响 Activity 的行为,但它不是很直观(不像检查是浅绿色时)。

我怎样才能保持完全相同的行为,但使用彩色复选框?

或者换句话说,是否可以(并且简单)使用android:enabled="false" 并保持视图的颜色?

这里是复选框的 xml 代码:

<CheckBox android:id="@+id/config_can_spn_checkbox"
android:layout_marginRight="30dip"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:enabled="false"
android:focusable="false" />

BaseExpandableListAdapter 类:

公共类 ConfigCanAdapter 扩展 BaseExpandableListAdapter {

private Context context;
private List<Pgn> pgnList;
private LayoutInflater inflater;

public ConfigCanAdapter(Context context, List<Pgn> pgnList) {
    this.context = context;
    this.pgnList = pgnList;
    inflater = LayoutInflater.from(context);
}

@Override
public boolean areAllItemsEnabled() {
    return true;
}

public Spn getChild(int gPosition, int cPosition) {
    return pgnList.get(gPosition).getSpnList().get(cPosition);
}

public long getChildId(int gPosition, int cPosition) {
    return cPosition;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    final Spn spn = (Spn) getChild(groupPosition, childPosition);

    ChildViewHolder childViewHolder;

    if (convertView == null) {
        childViewHolder = new ChildViewHolder();

        convertView = inflater.inflate(R.layout.group_child, null);

        childViewHolder.textViewChildSpnName = (TextView) convertView.findViewById(R.id.config_can_spn_name_text);
        childViewHolder.textViewChildSpnUnit = (TextView) convertView.findViewById(R.id.config_can_spn_unit_text);
        childViewHolder.checkBoxChildSpn = (CheckBox) convertView.findViewById(R.id.config_can_spn_checkbox); 

        convertView.setTag(childViewHolder);
    } else {
        childViewHolder = (ChildViewHolder) convertView.getTag();
    }

    childViewHolder.textViewChildSpnName.setText(spn.getName());

    childViewHolder.textViewChildSpnUnit.setText("(" + spn.getUnit() + ")");        

    return convertView;
}

public int getChildrenCount(int gPosition) {
    return pgnList.get(gPosition).getSpnList().size();
}

public Object getGroup(int gPosition) {
    return pgnList.get(gPosition);
}


public int getGroupCount() {
    return pgnList.size();
}

public long getGroupId(int gPosition) {
    return gPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    GroupViewHolder gholder;
    Pgn pgn = (Pgn) getGroup(groupPosition);

    if (convertView == null) {
        gholder = new GroupViewHolder();

        convertView = inflater.inflate(R.layout.group_row, null);

        gholder.textViewGroup = (TextView) convertView.findViewById(R.id.TVGroup);

        convertView.setTag(gholder);
    } else {
        gholder = (GroupViewHolder) convertView.getTag();
    }

    gholder.textViewGroup.setText(pgn.getId());

    return convertView;
}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}

class GroupViewHolder {
    public TextView textViewGroup;
}

class ChildViewHolder {
    public TextView textViewChildSpnName;
    public TextView textViewChildSpnUnit;
    public CheckBox checkBoxChildSpn;
}

}

最后是 onChildClickListener :

expandableList.setOnChildClickListener(new OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub
        CheckBox cb = (CheckBox)v.findViewById(R.id.config_can_spn_checkbox);
        cb.toggle();
        Toast.makeText(getBaseContext(), "Group = " + String.valueOf(groupPosition) + " child = " + String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
        return false;
    }
});

【问题讨论】:

    标签: java android xml checkbox


    【解决方案1】:

    您可以只设置android:clickable="false" 和android:focusable="false",这将带走所有点击,它不会获得焦点,但保持enabled 看起来像你想要的样子

    【讨论】:

    • 据我了解,android:focusable="false" 将焦点从复选框中移除,允许使用onChildClickListener。 android:clickable="false" 删除了onClickListeners 的触发器,但它不会阻止用户单击复选框。因此,状态发生了变化,但onChildClickListener 的代码没有被触发。这就是我基本上想要避免的
    【解决方案2】:

    您可以将按钮的选择器覆盖为如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:state_enabled="false" android:drawable="@drawable/image_enabled" />
       <item android:drawable="@drawable/image_enabled" />
    </selector>
    

    不确定仅此代码是否适合您,但这是一个想法 :) 请查看 here 和 here 获取教程。

    【讨论】:

    • 事实上,它也不起作用,因为drawables仍然会显示为灰色......
    • 不,drawables 会按照你的意愿出现......记住你正在覆盖它们。就像您从头开始制作自定义按钮一样。
    • 也查看这个帖子:stackoverflow.com/questions/5790454/…
    【解决方案3】:

    您可以使用点击侦听器来拦截点击并将值更改回来,而不是禁用该复选框。这听起来有点像黑客。您也可以选择子类化视图并以这种方式覆盖不良行为。

    【讨论】:

    • 我试图拦截对复选框的点击,但不知何故,它不起作用(框没有变回)
    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 2019-07-20
    • 2021-08-21
    • 1970-01-01
    • 2013-03-14
    • 2017-01-24
    • 2014-11-06
    • 2018-10-31
    相关资源
    最近更新 更多