无法使用自定义首选项注册Click事件

时间:2012-04-12 18:52:42

标签: android android-preferences

我构建了一个自定义Preference,其中包含Button字段的android:widgetLayout。我想在按下此按钮时触发特定操作,但是我无法让听众接收点击事件。这是我尝试过的:

    @Override
    public void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        addPreferencesFromResource( R.xml.preference_account );

        Preference signOutPreference = findPreference( "sign_out" );
        signOutPreference.setOnPreferenceClickListener( new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick( Preference preference ) {

                Log.i( TAG, "Click" );
                return false;
            }
        } );
    }

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {

        View root = super.onCreateView( inflater, container, savedInstanceState );
        root.findViewById( R.id.preference_signOut_Button ).setOnClickListener( new OnSignOutClickListener() );

        return root;
    }

    private class OnSignOutClickListener implements OnClickListener {

        @Override
        public void onClick( View v ) {

            Log.i( TAG, "Click" );
        }
    }

单击按钮或首选项行时,OnPreferenceClickListener永远不会被触发,onCreateView实现在尝试查找按钮视图时会抛出NullPointerException

如何点击此按钮时收听?作为参考,以下是相关的XML文件:

preference_account.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <Preference
        android:key="sign_out"
        android:persistent="false"
        android:summary="account@email.com"
        android:title="Signed in as:"
        android:widgetLayout="@layout/sign_out_button" />

</PreferenceScreen>

sign_out_button.xml

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/preference_signOut_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sign Out" />

修改:我找到了使用该按钮的android:onClick字段的解决方案,但如果可能,我仍然希望使用Preference的解决方案。

2 个答案:

答案 0 :(得分:0)

也许您可以尝试以下示例:

class MyOnPreferenceClickListener implements OnPreferenceClickListener {
private Activity activity;
private static final String TAG = "ABCDEFGH...";

public MyOnPreferenceClickListener(Activity activity) {
    this.activity = activity;
}

@Override
public boolean onPreferenceClick(Preference preference) {
    Log.i(TAG, "Click on Item " + preference.getKey());
    return true;
}
}

答案 1 :(得分:0)

更新@Sushanth Raghuthaman回答一下。这对我有用:

public class CustomPreference extends Preference implements OnPreferenceClickListener {

    public CustomPreference(Context context) {
        super(context);
        setOnPreferenceClickListener(this);
    }

    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnPreferenceClickListener(this);
    }

    public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setOnPreferenceClickListener(this);
    }

    public boolean onPreferenceClick(Preference preference) {
        //Do something here...
        return true;
    }

}

上面重要的一行是注册首选项单击 setOnPreferenceClickListener(this);