Android:将OnClick处理程序添加到自定义首选项布局

时间:2014-01-26 03:14:13

标签: android imagebutton preference

我在某个未知领域。不幸的是,我并不完全了解Android的结构。我正在尝试为用户创建一种使用首选项布局添加联系人的方法。我的应用程序添加了联系人,但我正在努力从列表中删除联系人。我为我的首选对象创建了一个自定义布局。

contact_pref_layout.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="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
    <RelativeLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_marginLeft="15dip"
    android:layout_marginRight="6dip" android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip" android:layout_weight="1">
    <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
        android:ellipsize="marquee" android:fadingEdge="horizontal"
        android:textColor="#FF0000" />

    <TextView android:id="@+android:id/summary"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:maxLines="4" />
    <ImageButton android:id="@+id/btnDelete"
        android:layout_width="32dp" 
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/trash_can"
        />
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical" android:orientation="vertical" />
    </RelativeLayout>
</LinearLayout>

我正在动态创建我添加联系人的所有首选项对象。

添加联系人功能:

   private void addContact(String contactName, String contactPhone)
    {
        Log.i(TAG,"in addContact Function");

        Toast.makeText(this, contactName + "'s phone number found: " + contactPhone , Toast.LENGTH_SHORT).show();

        PreferenceCategory targetCategory = (PreferenceCategory)findPreference("contact_category");

        Preference newContact = new Preference(this);
        newContact.setKey("contact123");
        newContact.setTitle(contactName);
        newContact.setSummary(contactPhone);
        newContact.setLayoutResource(R.layout.contact_pref_layout);


        Log.i(TAG,"Add new Contact");
        targetCategory.addPreference(newContact);

    }

我不确定在哪里或如何为要删除的ImageButton添加onClickListener。我甚至不确定如何删除Preference对象。 有什么建议吗?

EDIT 1/26/2014 LOGCAT

01-26 08:22:36.846: E/AndroidRuntime(18987): FATAL EXCEPTION: main
01-26 08:22:36.846: E/AndroidRuntime(18987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleemergencywidget/com.example.simpleemergencywidget.ContactPrefActivity}: java.lang.NullPointerException
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.os.Looper.loop(Looper.java:137)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.ActivityThread.main(ActivityThread.java:5455)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at java.lang.reflect.Method.invokeNative(Native Method)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at java.lang.reflect.Method.invoke(Method.java:525)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at dalvik.system.NativeStart.main(Native Method)
01-26 08:22:36.846: E/AndroidRuntime(18987): Caused by: java.lang.NullPointerException
01-26 08:22:36.846: E/AndroidRuntime(18987):    at com.example.simpleemergencywidget.ContactPrefActivity.onCreate(ContactPrefActivity.java:36)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.Activity.performCreate(Activity.java:5372)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-26 08:22:36.846: E/AndroidRuntime(18987):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-26 08:22:36.846: E/AndroidRuntime(18987):    ... 11 more

创建联系页面活动时出现此错误。

2 个答案:

答案 0 :(得分:1)

我最终使用添加

android:onClick="deleteContact"

到布局并添加了deleteContact函数。

答案 1 :(得分:0)

如何为ImageButton设置onClickListener:

ImageButton deleteButton = (ImageButton) findViewById(R.id.btnDelete);
deleteButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //code that deletes the Preference object
    }
});

我没有对Preference对象做过任何事情,所以我不能告诉你如何从我的noggin顶部删除它们。这应该很容易在文档中找到。