0

我的问题是,如果我按下我的列表项之一,复选框只会在我的手指触摸它时被选中。我一直在疯狂地寻找,但我很难相信没有简单的解决方法。

这是我的代码示例。

我正在为我的列表项使用以下 XML:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"

android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#000000"
android:textSize="25dp"
/>

附加到此类,它正在我的阵列适配器中使用。

public class Misshandel extends Activity {

    Button misstankt;
    ListView mListView;
    ArrayAdapter<String> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forhor);

        misstankt = (Button) findViewById(R.id.misstanktKnapp);
        mListView = (ListView) findViewById(R.id.forhorsLista);





         final String[] misshandelMT = getResources().getStringArray(R.array.misshandelMT);

        misstankt.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(Misshandel.this, R.layout.custom_list_item, misshandelMT);
                mListView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();

            }
        });
}
}

总而言之,我想要实现的是能够检查我的框!

如果您想查看,这里是完整的课程:http: //codepaste.net/fg3xiz

谢谢你的时间!

4

1 回答 1

0

您应该发布更详细的信息,但我会给您我的对话框代码,您可以在其中选择几个选项:

`multiselect.xml'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" >

    <ListView
        android:id="@+id/values"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="2"
        android:choiceMode="multipleChoice" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/selectAll"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:text="@string/select_all" />

        <Button
            android:id="@+id/ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/ok" />

        <Button
            android:id="@+id/deselectAll"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/select_none" />
    </LinearLayout>
</LinearLayout>

以及 Dialog 类(支持翻译奖励):

public class MultiSelectDialog extends Dialog {
    private final String title;
    private ListView items;
    private List<String> selectedItems;
    private HashMap<String, String> translation;
    private List<String> displayedValues;

    public MultiSelectDialog(Context context, String title, List<String> values,
            HashMap<String, String> translation) {
        super(context);
        this.title = title;
        this.translation = translation;
        if(translation != null) {
            displayedValues = FUtils.collect(translation.entrySet().iterator(),
                    new Converter<HashMap.Entry<String, String>, String>() {
                        public String convert(Entry<String, String> object) {
                            return object.getValue();
                        }
                    });
        }
        else {
            displayedValues = values;
        }
        selectedItems = new LinkedList<String>();
    }

    public List<String> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(List<String> selectedItems) {
        this.selectedItems.clear();
        this.selectedItems.addAll(selectedItems);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multiselect);

        setTitle(title);
        items = (ListView) findViewById(R.id.values);

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_multiple_choice, displayedValues);
        items.setAdapter(adapter);
        for(int i = 0; i < adapter.getCount(); ++i) {
            items.setItemChecked(i, false);
        }

        if(selectedItems != null) {
            for(int i = 0; i < selectedItems.size(); ++i) {
                String value = getTranslatedValue(selectedItems.get(i));
                for(int j = 0; j < items.getAdapter().getCount(); ++j) {
                    if(items.getAdapter().getItem(j).equals(value)) {
                        items.setItemChecked(j, true);
                    }
                }
            }
        }

        Button selectAll = (Button) findViewById(R.id.selectAll);
        selectAll.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for(int i = 0; i < adapter.getCount(); ++i) {
                    items.setItemChecked(i, true);
                }
            }
        });
        Button deselectAll = (Button) findViewById(R.id.deselectAll);
        deselectAll.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for(int i = 0; i < adapter.getCount(); ++i) {
                    items.setItemChecked(i, false);
                }
            }
        });
        Button okButton = (Button) findViewById(R.id.ok);
        okButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                selectedItems.clear();
                for(int i = 0; i < adapter.getCount(); ++i) {
                    if(items.isItemChecked(i)) {
                        String item = (String) items.getItemAtPosition(i);
                        reverseTranslateAndAdd(item);
                    }
                }
                dismiss();
            }
        });
    }

    private String getTranslatedValue(String key) {
        if(translation == null) {
            return key;
        }
        return translation.get(key);
    }

    private void reverseTranslateAndAdd(String item) {
        if(translation != null) {
            for(Entry<String, String> e : translation.entrySet()) {
                String value = e.getValue();
                if(value.equals(item)) {
                    selectedItems.add(e.getKey());
                    break;
                }
            }
        }
        else {
            selectedItems.add(item);
        }
    }
}
于 2012-05-31T18:59:45.543 回答