2015-09-16 63 views
0

我是新開發的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

您的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(); 
} 
+0

我已經添加它。但它不起作用:( – Krishan

+0

「key = update_profile_key」和'key =「按鈕」的首選項意味着是單個首選項? – headuck

+0

不,有兩種首選項:首選項是key =「button」像按鈕和其他工作不是.. – Krishan