将片段作为按钮的PreferenceScreen

时间:2016-11-12 18:33:03

标签: android button fragment preferencescreen

在我的PreferenceScreen上,我想要一个返回按钮。 我找到了很多关于如何在PreferenceScreen中创建按钮的链接。 我试图实现解决方案,该解决方案包括在Preference标记中导入布局,但是我的代码中的setOnPreferenceClickListener的实现不起作用,当我点击它时没有效果。我在Preference标签中使用了一个片段。

PreferenceScreen:

<Preference
    android:key="fragment_return_key"
    android:layout="@layout/fragment_return"
    ></Preference>

片段:

    <RelativeLayout android:id="@+id/lytopnavreturn"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp">
    <ImageButton
        android:id="@+id/btnfragnavreturn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:src="@drawable/return_32"
        android:background="@android:color/transparent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"/>
    <TextView
        android:id="@+id/txtfragnavreturnsettings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_settings"
        android:textSize="18dp"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryDark"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />
</RelativeLayout>   

活动OnCreate代码:

Preference button2 = (Preference)findPreference("fragment_return_key");
    button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            onBackPressed();
            return true;
        }
    });

显示片段,但点击时不会启动事件。

感谢。

0 个答案:

没有答案