0

我是android开发的新手。我创建了一个 PreferenceFragment。页面底部有一个按钮(Preference)。将自定义布局添加到按钮后,该按钮不可点击。但是在按钮之外(在偏好内是可点击的)请帮我解决这个问题

<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;
}

}

4

1 回答 1

1

Your button custom preference does not specify a layout. You may need to specify a layout and set android:focusable="false". See Custom preference not clickable

Edit: For the @layout/update_profile_btn file, you may try the following modifications:

<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>

and define the method

void onButtonClick(View v) {
   Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
}
于 2015-09-16T18:08:33.700 回答