android - 将简单多重选择微调器转换为自定义多重选择微调器

标签 android spinner customization multipleselection

我创建了一个多重选择微调器示例。效果很好。但现在我想自定义微调器行。不知道怎么改

new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item)

我需要定制:

  • 背景颜色
  • 复选框背景
  • 文字颜色

enter image description here

我该怎么办?非常感谢

public class MultiSelectionSpinner extends Spinner implements OnMultiChoiceClickListener {  
String[] _items = null;  
boolean[] mSelection = null;  

ArrayAdapter<String> simple_adapter;  

public MultiSelectionSpinner(Context context) {  
    super(context);  

    simple_adapter = new ArrayAdapter<String>(context,  
            android.R.layout.simple_spinner_item);  
    super.setAdapter(simple_adapter);  
}  

public MultiSelectionSpinner(Context context, AttributeSet attrs) {  
    super(context, attrs);  

    simple_adapter = new ArrayAdapter<String>(context,  
            android.R.layout.simple_spinner_item);  
    super.setAdapter(simple_adapter);  
}  

public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
    if (mSelection != null && which < mSelection.length) {  
        mSelection[which] = isChecked;  

        simple_adapter.clear();  
        simple_adapter.add(buildSelectedItemString());  
    } else {  
        throw new IllegalArgumentException(  
                "Argument 'which' is out of bounds.");  
    }  
}  

@Override  
public boolean performClick() {  
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());  
    builder.setMultiChoiceItems(_items, mSelection, this);  
    builder.show();  
    return true;  
}  

@Override  
public void setAdapter(SpinnerAdapter adapter) {  
    throw new RuntimeException(  
            "setAdapter is not supported by MultiSelectSpinner.");  
}  

public void setItems(String[] items) {  
    _items = items;  
    mSelection = new boolean[_items.length];  
    simple_adapter.clear();  
    simple_adapter.add(_items[0]);  
    Arrays.fill(mSelection, false);  
}  

public void setItems(List<String> items) {  
    _items = items.toArray(new String[items.size()]);  
    mSelection = new boolean[_items.length];  
    simple_adapter.clear();  
    simple_adapter.add(_items[0]);  
    Arrays.fill(mSelection, false);  
}  

public void setSelection(String[] selection) {  
    for (String cell : selection) {  
        for (int j = 0; j < _items.length; ++j) {  
            if (_items[j].equals(cell)) {  
                mSelection[j] = true;  
            }  
        }  
    }  
}  

public void setSelection(List<String> selection) {  
    for (int i = 0; i < mSelection.length; i++) {  
        mSelection[i] = false;  
    }  
    for (String sel : selection) {  
        for (int j = 0; j < _items.length; ++j) {  
            if (_items[j].equals(sel)) {  
                mSelection[j] = true;  
            }  
        }  
    }  
    simple_adapter.clear();  
    simple_adapter.add(buildSelectedItemString());  
}  

public void setSelection(int index) {  
    for (int i = 0; i < mSelection.length; i++) {  
        mSelection[i] = false;  
    }  
    if (index >= 0 && index < mSelection.length) {  
        mSelection[index] = true;  
    } else {  
        throw new IllegalArgumentException("Index " + index  
                + " is out of bounds.");  
    }  
    simple_adapter.clear();  
    simple_adapter.add(buildSelectedItemString());  
}  

public void setSelection(int[] selectedIndicies) {  
    for (int i = 0; i < mSelection.length; i++) {  
        mSelection[i] = false;  
    }  
    for (int index : selectedIndicies) {  
        if (index >= 0 && index < mSelection.length) {  
            mSelection[index] = true;  
        } else {  
            throw new IllegalArgumentException("Index " + index  
                    + " is out of bounds.");  
        }     
    }  
    simple_adapter.clear();  
    simple_adapter.add(buildSelectedItemString());  
}  

public List<String> getSelectedStrings() {  
    List<String> selection = new LinkedList<String>();  
    for (int i = 0; i < _items.length; ++i) {  
        if (mSelection[i]) {  
            selection.add(_items[i]);  
        }  
    }  
    return selection;  
}  

public List<Integer> getSelectedIndicies() {  
    List<Integer> selection = new LinkedList<Integer>();  
    for (int i = 0; i < _items.length; ++i) {  
        if (mSelection[i]) {  
            selection.add(i);  
        }  
    }  
    return selection;  
}  

private String buildSelectedItemString() {  
    StringBuilder sb = new StringBuilder();  
    boolean foundOne = false;  

    for (int i = 0; i < _items.length; ++i) {  
        if (mSelection[i]) {  
            if (foundOne) {  
                sb.append(", ");  
            }  
            foundOne = true;  

            sb.append(_items[i]);  
        }  
    }  
    return sb.toString();  
}  

public String getSelectedItemsAsString() {  
    StringBuilder sb = new StringBuilder();  
    boolean foundOne = false;  

    for (int i = 0; i < _items.length; ++i) {  
        if (mSelection[i]) {  
            if (foundOne) {  
                sb.append(", ");  
            }  
            foundOne = true;  
            sb.append(_items[i]);  
        }  
    }  
    return sb.toString();  
}  
}  

最佳答案

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                         context, 
                         android.R.layout.simple_list_item_multiple_choice){
                    @Override
                    public View getView(int position, View convertView,
                            ViewGroup parent) {
                        View view =super.getView(position, convertView, parent);

                        CheckedTextView textView=(CheckedTextView) view.findViewById(android.R.id.text1);

                        textView.setCheckMarkDrawable(0);                       
                        textView.setCompoundDrawablesWithIntrinsicBounds (0, 0,R.drawable.checkbox_selector ,0);
                        textView.setTextColor(getResources().getColor(R.color.YOUR_COLOR));
                        //set background color for view
                        return view;
                    }
                };

drawable-->checkbox_selector.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

     <item android:drawable="@drawable/CHECKEDIMAGE" android:state_checked="true"/>
        <item android:drawable="@drawable/CHECKEDIMAGE" android:state_focused="true"/>
        <item android:drawable="@drawable/UNCHECKEDIMAGE"/>

    </selector>

关于android - 将简单多重选择微调器转换为自定义多重选择微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357323/

相关文章:

java - 在 Eclipse 中快速修复定制可能吗?

android - 如何在 Evernote 应用程序中制作这样的下拉菜单?

java - 媒体播放器错误 -38,0

java - 了解 Arrayadapter 示例

java - 当我在第一个微调器中再次选择一个项目时发生错误,然后相关数据不会在第二个微调器中解析意味着它不会刷新

android 微调器-onItemSelected

css - 谷歌地图平移和缩放自定义颜色

android - 如何保护 AWS 的键值

android - 从android中的mysql数据库加载多个微调器

java - 使用 JUnit assertEquals 的自定义异常消息?