自定义布局的首选项不可单击

时间:2015-09-16 17:30:26

标签: android android-preferences preferencefragment

我是Android开发的新手。我创建了一个PreferenceFragment。页面底部有一个按钮(首选项)。在将自定义布局添加到按钮后,该按钮不可单击。但是在按钮之外(优先选择是可点击的)请帮我解决这个问题

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:key="@string/header_user_detail"
    android:title="YOUR DETAIL">

    <EditTextPreference
        android:key="user_name"
        android:title="@string/user_name"></EditTextPreference>
    <com.empite.oneonus.helpers.custom_preferences.DatePreference
        android:key="dob"
        android:title="@string/date_of_birth"></com.empite.oneonus.helpers.custom_preferences.DatePreference>
    <EditTextPreference
        android:key="work_postcode"
        android:title="@string/work_postcode"></EditTextPreference>
    <EditTextPreference
        android:key="home_postcode"
        android:title="@string/home_postcode"></EditTextPreference>
    <ListPreference
        android:dialogTitle="Application language"
        android:key="work_category"
        android:summary="Select the Application language"
        android:title="Language"></ListPreference>
    <Preference
        android:key="update_profile_key"
        android:layout="@layout/update_profile_btn"></Preference>
    <Preference
        android:key="button"
        android:summary="This will act like a button"
        android:title="Acts like a button" />
</PreferenceCategory>

<PreferenceCategory
    android:key="@string/header_user_account"
    android:title="YOUR ACCOUNT">
    <EditTextPreference
        android:key="edit_email"
        android:layout="@layout/edit_email_address"></EditTextPreference>
    <EditTextPreference
        android:key="edit_password"
        android:layout="@layout/edit_password"></EditTextPreference>
</PreferenceCategory>

布局/ update_profile_btn

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

<Button
    android:id="@+id/update_profile_btn"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="10dp"
    android:background="@color/btn_light_green_color"
    android:focusable="false"
    android:text="Update Profile"
    android:textAllCaps="false"
    android:textColor="@color/white"></Button>

public class UpdateProfilePrefFragment extends com.github.machinarius.preferencefragment.PreferenceFragment implements Preference.OnPreferenceClickListener {

protected static void setListPreferenceData(ListPreference lp) {
    CharSequence[] entries = {"English", "French"};
    CharSequence[] entryValues = {"1", "2"};
    lp.setEntries(entries);
    lp.setDefaultValue("1");
    lp.setEntryValues(entryValues);
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.update_profile);
    final ListPreference listPreference = (ListPreference) findPreference("work_category");
    setListPreferenceData(listPreference);
    listPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {

            setListPreferenceData(listPreference);
            return false;
        }
    });

    Preference pref = findPreference("update_profile_key");
    pref.setOnPreferenceClickListener(this);
    Preference button = (Preference) findPreference("button");
    button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference arg0) {
            Toast.makeText(getActivity(), "button", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

@Override
public boolean onPreferenceClick(Preference preference) {
    Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
    return false;
}

}

1 个答案:

答案 0 :(得分:1)

您的button自定义偏好设置未指定布局。您可能需要指定布局并设置android:focusable="false"。见Custom preference not clickable

编辑:对于@layout/update_profile_btn文件,您可以尝试以下修改:

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

<Button
    android:id="@+id/update_profile_btn"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="10dp"
    android:background="@color/btn_light_green_color"
    android:onClick="onButtonClick"
    android:text="Update Profile"
    android:textAllCaps="false"
    android:textColor="@color/white"></Button>

</RelativeLayout>

并定义方法

void onButtonClick(View v) {
   Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
}