首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:仅在选中第一个复选框时才启用第二个复选框

Android:仅在选中第一个复选框时才启用第二个复选框
EN

Stack Overflow用户
提问于 2016-06-15 13:15:22
回答 4查看 432关注 0票数 0

我正在使用一个自定义数组适配器,并在同一行中有两个复选框。只有在选中第一个选项时,才应该启用第二个选项。我不知道如何从第一个列表的onClickListener获得第二个列表框的句柄。

代码语言:javascript
复制
    private class MyCustomAdapter extends ArrayAdapter<DiatonicMajorKey> {

    private ArrayList<DiatonicMajorKey> keyList;

    public MyCustomAdapter(Context context, int textViewResourceId,
                           ArrayList<DiatonicMajorKey> keyList) {
        super(context, textViewResourceId, keyList);
        this.keyList = new ArrayList<DiatonicMajorKey>();
        this.keyList.addAll(keyList);
    }

    // this class holds the items that actually appear on the screen.
    private class ViewHolder {
        TextView code;
        CheckBox key;
        CheckBox inversion;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.key_info, null);


            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.keycode);
            holder.key = (CheckBox) convertView.findViewById(R.id.checkBox1);
            holder.inversion = (CheckBox) convertView.findViewById(R.id.checkBox2);
            convertView.setTag(holder);

            holder.key.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    DiatonicMajorKey _key = (DiatonicMajorKey) cb.getTag();
                    _key.setKeySelected(cb.isChecked());

                    // how to get handle of inversion checkbox to enable/disable it
                }
            });
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-06-15 14:12:09

好的,明白了:

代码语言:javascript
复制
            final CheckBox other=holder.inversion;

            convertView.setTag(holder);

            holder.inversion.setEnabled(false);

            // set checkchanged listener to your first checkbox
            holder.key.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        //enable if the first checkbox is checked/ticked
                        other.setEnabled(true);
                    }
                }
            });


            holder.key.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    DiatonicMajorKey _key = (DiatonicMajorKey) cb.getTag();
                    _key.setKeySelected(cb.isChecked());

                    // handle of inversion checkbox to enable/disable it
                    other.setEnabled(cb.isChecked());
                    other.setChecked(false);
                }
            });

谢谢你的帮助!

票数 0
EN

Stack Overflow用户

发布于 2016-06-15 13:21:00

假设您有两个名为checkBoxFirst和checkBoxSecond的复选框。

代码语言:javascript
复制
checkBoxFirst.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkBoxSecond.setEnabled(isChecked);
            }
        });

希望它能帮助你:)

票数 1
EN

Stack Overflow用户

发布于 2016-06-15 13:21:33

尝尝这个

代码语言:javascript
复制
 //initialize the second checkbox and set enabled to false 
 holder.inversion.setEnabled(false);

  // set checkchanged listener to your first checkbox
    holder.key.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
               //enable if the first checkbox is checked/ticked
                holder.inversion.setEnabled(true);
            }
        }
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37836559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档