11

我正在尝试为我的应用程序制作多个联系人选择器。我正在使用带有复选框的自定义布局来选择多个联系人。我面临的问题是,为了选择任何特定的联系人,我需要准确地点击复选框,如果我点击行的其他部分,复选框不会被勾选。我如何确保用户在整行中单击的任何位置都选中了复选框。

activity_contacts_picker.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btnShow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Selected" />

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/btnShow" >
</ListView>

</RelativeLayout>

custcontactview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >

<TextView
    android:id="@+id/txtContactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15.0dip"
    android:layout_toLeftOf="@+id/checkBox1"
    android:layout_alignParentLeft="true"
    android:text="Medium Text"
    android:textAppearance="?android:textAppearanceMedium" />

<TextView
    android:id="@+id/txtContactNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtContactName"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15.0dip"
    android:layout_toLeftOf="@+id/checkBox1"
    android:text="Small Text"
    android:textAppearance="?android:textAppearanceSmall" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="false" />

</RelativeLayout>

ContactsPicker.java

public class ContactsPicker extends ListActivity {

protected Object mActionMode;
public int selectedItem = -1;
private Button btnShowContacts;
private ListView myListView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_picker);
    myListView = getListView();
    btnShowContacts = (Button) findViewById(R.id.btnShow);
    btnShowContacts.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Button Clicked",
                    Toast.LENGTH_SHORT).show();
            String name = null;
            String number = null;
            long[] ids = myListView.getCheckedItemIds();
            for (long id : ids) {
                Cursor contact = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id + "" }, null);
                while (contact.moveToNext()) {
                    name = contact.getString(contact
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    number = contact.getString(contact
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                Toast.makeText(getApplicationContext(),
                        "Name: " + name + "\n" + "Number: " + number,
                        Toast.LENGTH_LONG).show();
            }
        }
    });
    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "Name", "Phone" };
    int[] to = { R.id.txtContactName, R.id.txtContactNumber };
    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.custcontactview, from, to);
    setListAdapter(adapter);
}

private ArrayList<Map<String, String>> buildData() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
    list.clear();
    Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    while (people.moveToNext()) {
        String contactName = people.getString(people
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people
                .getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people
                .getString(people
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        if ((Integer.parseInt(hasPhone) > 0)) {
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + contactId,
                    null,
                    "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                            + ") ASC");
            while (phones.moveToNext()) {
                String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Map<String, String> NamePhoneType = new HashMap<String, String>();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);
                list.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    //startManagingCursor(people);
    return list;
}
}
4

5 回答 5

11

尝试将此属性添加到您的 ListView

安卓:可点击=“真”

另外,我看不到您将触摸/单击侦听器设置到 myListView 的位置。

于 2012-10-13T04:38:56.443 回答
6

尝试更改复选框布局宽度以填充父级,如下所示,

 android:layout_width="fill_parent"

猜猜它会为你工作:)

于 2012-10-13T06:29:02.577 回答
1

我会在文本字段中添加一个单击侦听器,如果单击,则将复选框的状态更改为相反的状态(单击 <-> 未单击)

于 2012-10-13T04:42:58.810 回答
1

首先,您可以使用 CheckedTextView 代替 CheckBox。也许只有这个工作。

或者

尝试设置 CheckBox focusable="false",如果不起作用,则可以在 XML 布局的所有视图中聚焦。

于 2012-10-13T05:03:20.020 回答
0

如果您在列表项上使用按钮/复选框/单选按钮,则列表项不可点击

您可以点击这里解决您的问题

于 2012-10-13T06:31:30.650 回答