Android以编程方式关闭自定义首选项

时间:2017-04-11 17:37:24

标签: android customization android-preferences

我有这个自定义类来消费偏好。在内部,在布局中我有一个按钮,我想要在单击按钮时关闭首选项并显示父屏幕。任何人都知道如何存档这个?

偏好布局:

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

<Button
    android:id="@+id/piaButtonClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:text="Close" />

类别:

public class PrefAccountInsert extends Preference {

    public PrefAccountInsert(Context context) {
        super(context);
        init();
    }

    public PrefAccountInsert(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PrefAccountInsert(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setLayoutResource(R.layout.pref_insert_account);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        Button button = (Button) holder.findViewById(R.id.piaButtonClose);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 // Close this when clicked and show parent screen in this case show "Account and Security"
            }
        });
    }
}

首选项xml:

<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory android:title="Account and Security">
    <PreferenceScreen
        android:key="@string/screen_preference_account_and_security"
        android:summary="Add or remove account"
        android:title="Account and Security">
        <PreferenceScreen
            android:key="@string/screen_preference_account_insert"
            android:summary="Add new account"
            android:title="Add account">
            <com.eb91.ares.memos.appSettings.prefs.accountAndSecurity.PrefAccountInsert
                android:key="@string/pref_account_insert"/>
        </PreferenceScreen>
    </PreferenceScreen>
</PreferenceCategory>

0 个答案:

没有答案