【问题标题】:Preference with custom layout not clickable自定义布局的偏好不可点击
【发布时间】:2015-09-16 17:30:25
【问题描述】:

我是安卓开发新手。我创建了一个 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;
}

}

【问题讨论】:

    标签: android android-preferences preferencefragment


    【解决方案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();
    }
    

    【讨论】:

    • 带有key=update_profile_key 的偏好和带有key="button" 的偏好是否意味着是一个单一的偏好项目?
    • 不,有两种偏好。偏好 key="button" 像按钮一样工作,而其他则不是..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多