2013-03-31 15 views
5

Ho una schermata di preferenza e sto specificando una risorsa di layout utilizzando l'attributo android:layout. Nell'attività di preferenza sto usando/gonfiando la preferenza usando addPreferencesFromResource dal metodo onCreate. All'interno del layout ho poche visualizzazioni di testo e pulsanti.utilizzo di findviewbyid in preferenceactivity

Il problema è che non riesco a trovare nessuna delle visualizzazioni di testo, caselle di controllo e pulsanti utilizzando il metodo findViewById! Ogni risultato per findViewById restituisce null.

EDIT:

Ho bisogno di allegare un listener per la casella di controllo.

Qualcuno può aiutarmi per favore qui?

EDIT 2:

1) preference_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="33dp" 
    android:gravity="center_vertical" 
    android:minHeight="33dp" 
    android:id="@+id/preference_header_switch_item_responder_layout" 
    style="?android:attr/listSeparatorTextViewStyle" > 

    <CheckBox 
    android:id="@+id/switchWidget" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:clickable="true" 
    android:focusable="false" 
    android:padding="8dip" 
    android:layout_marginRight="14dip" /> 
</LinearLayout> 

2) preferenza schermo

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <Preference 
     android:defaultValue="false" 
     android:key="auto_responder_key" 
     android:widgetLayout="@layout/preference_layout" 
     android:title="@string/auto_responder"> 
    </Preference> 

</PreferenceScreen> 

3) Preferenza Attività onCreate:

EDIT 3:

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     if(savedInstanceState != null && savedInstanceState.containsKey(HEADERS_KEY)) 
     { 
      headers = savedInstanceState.getParcelableArrayList(HEADERS_KEY); 
     } 

     super.onCreate(savedInstanceState); 
     applicationContext = getApplicationContext(); 

     setupSimplePreferencesScreen(); 
//  setContentView(getListView()); 
     headerCompound = findViewById(R.id.switchWidget); 
     headerCompoundEmail = findViewById(R.id.switchWidgetEmail); 
    } 

e

private void setupSimplePreferencesScreen() 
    { 
     if (!isSimplePreferences(this)) 
     { 
      return; 
     } 

     addPreferencesFromResource(R.xml.responder_generic); 

    PreferenceCategory fakeHeader = new PreferenceCategory(this); 

     fakeHeader.setLayoutResource(R.layout.place_holder_auto_reply_header); 

     getPreferenceScreen().addPreference(fakeHeader); 
     addPreferencesFromResource(R.xml.auto_reply_sms_call_check_box_preference); 
     addPreferencesFromResource(R.xml.auto_reply_preferences); 

     Preference smsTemplatePreference = findPreference(SMS_TEMPLATE_KEY); 
     if(smsTemplatePreference != null) 
     { 
      bindPreferenceSummaryToValue(smsTemplatePreference); 
     } 

     Preference customTemplatePreference = findPreference(SMS_CUSTOM_TEMPLATE_KEY); 
     if(customTemplatePreference != null) 
     { 
      customTemplatePreference.setEnabled(false); 
      bindPreferenceSummaryToValue(customTemplatePreference); 
      getPreferenceScreen().removePreference(customTemplatePreference); 
     } 

     // Add 'email' preferences, and a corresponding header. 

     // Add 'email header' preferences. 
     addPreferencesFromResource(R.xml.email_enable_check); 
     addPreferencesFromResource(R.xml.email_preferences); 

     Preference passwordPreference = findPreference(EMAIL_PWD_KEY); 

     if(passwordPreference != null) 
     { 
      bindPreferenceSummaryToValue(passwordPreference); 
      // the advanced version of SMS template 
     } 

     Preference customPasswordPreference = findPreference(EMAIL_CUSTOM_PWD_KEY); 
     if(customPasswordPreference != null) 
     { 
      bindPreferenceSummaryToValue(customPasswordPreference); 
      getPreferenceScreen().removePreference(customPasswordPreference); 
     } 

     // Add 'network' preferences, and a corresponding header. 
     fakeHeader = new PreferenceCategory(this); 
     fakeHeader.setLayoutResource(R.layout.place_holder_network_header); 

     getPreferenceScreen().addPreference(fakeHeader); 
     addPreferencesFromResource(R.xml.network_preferences_check_box); 

     // Add 'misc' preferences, and a corresponding header. 
     fakeHeader = new PreferenceCategory(this); 
     fakeHeader.setLayoutResource(R.layout.place_holder_misc_header); 

     getPreferenceScreen().addPreference(fakeHeader); 
     addPreferencesFromResource(R.xml.misc_preferences); 
     addPreferencesFromResource(R.xml.misc_reject_ignore_check_box_preferences); 

     // Bind the summaries of EditText/Switch/CheckBox preferences to 
     // their values. When their values change, their summaries are updated 
     // to reflect the new value, per the Android Design guidelines. 
     bindPreferenceSummaryToValue(findPreference(AUTO_REPLY_SMS_KEY)); 
     bindPreferenceSummaryToValue(findPreference(AUTO_REPLY_CALL_KEY)); 
     bindPreferenceSummaryToValue(findPreference(DATA_NETWORK_KEY)); 
     bindPreferenceSummaryToValue(findPreference(WIFI_KEY)); 
     bindPreferenceSummaryToValue(findPreference(PASSCODE_KEY)); 
     bindPreferenceSummaryToValue(findPreference(EMAIL_USR_KEY)); 
     bindPreferenceSummaryToValue(findPreference(EMAIL_INT_KEY)); 
     bindPreferenceSummaryToValue(findPreference(AUTO_RESPONDER_KEY)); 
} 

ho provato il codice precedente in onPostCreate pure ma ancora il valore restituito è nullo.

Voglio trovare le caselle di controllo per responder_generic.xml e email_preferences.xml. C'è un modo per trovare e aggiungere ascoltatori a loro?

Grazie, Adithya

+0

puoi postare quello che hai provato –

+0

ho aggiunto il mio codice. – Adithya

+0

pls pubblica il tuo codice java completo –

risposta

2

Quello che ho fatto è stato creare una preferenza personalizzata e ha funzionato. Ho semplicemente esteso la classe Preference e ho gonfiato il layout fornito nel layout /% MY_LAYOUT% .xml. Venivano chiamati anche gli ascoltatori, che era quello che volevo ottenere.

Questa soluzione non era da nessuna parte, credo, quindi, aggiungendo questo alla comunità Wiki.

Nota: se è stata aggiunta una singola preferenza, è sufficiente single preference solution. Sto aggiungendo più preferenze sullo schermo (nel mio caso 2), ho implementato le preferenze personalizzate.

0

Da quello che avete chiesto Sembra che non siete in grado di ottenere qualsiasi visualizzazione quando si utilizza

findViewById() 

così una possibile causa di questo comportamento potrebbe essere che si non può aver chiamato

setContentView(R.id.yourlayout); 

il check this out nel codice ...

+0

effettivamente, ci sono varie preferenze che sto caricando sull'attività e un paio di loro hanno layout, quindi, non ho usato setContentView per un particolare layout. Stai suggerendo di setContentView per entrambi i layout uno dopo l'altro? Funzionerà ? Lo proverò comunque. – Adithya

0

Ho avuto lo stesso problema nel tentativo di inserire direttamente un gruppo radio in PreferenceScreen. Stavo cercando di impostare il pulsante RadioButton durante la creazione dell'attività (ad esempio ripristinando il valore di un booleano da sharedpreferences) ma non ero in grado di trovareViewById o simile dal metodo OnCreate. Ecco la mia soluzione (brutto ma di lavoro): ho esteso la classe RadioButton:

public class RadioButtonSettings extends RadioButton { 
public RadioButtonSettings(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    boolean isLocal = context.getSharedPreferences("",Context.MODE_PRIVATE).getBoolean("local",true); 
    switch(getId()){ 
     case R.id.radio_in: 
      if(isLocal) 
       setChecked(true); 
      break; 
     case R.id.radio_out: 
      if(!isLocal) 
       setChecked(true); 
      break; 
     default: 
      break; 
    } 
}} 

poi mettere qualche parte nel file preferences.xml (caricato dal PreferenceActivity):

<PreferenceCategory 
android:title="CATEGORY EXAMPLE"> 
<Preference android:layout="@xml/category_one" 
    android:selectable="false" 
    android:key="radiobuttons"> 
</Preference> 
</PreferenceCategory> 

e, infine, creare un altro file nella res/xml directory (denominata category_one.xml) e mettere:

<RadioGroup 
    android:id="@+id/radio" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <your.package.RadioButtonSettings 
     tools:context="your.package.SettingsActivity" 
     android:id="@+id/radio_in" 
     android:text="@string/radioIn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </your.package.RadioButtonSettings> 
    <your.package.RadioButtonSettings 
     tools:context="your.package.SettingsActivity" 
     android:id="@+id/radio_out" 
     android:text="@string/radioOut" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </your.package.RadioButtonSettings> 
</RadioGroup> 

Si potrebbe anche usare l'androide: onClick = "" opzione dal file xml per ottenere OnClickListener. Nel caso specifico è stato un semplice controllo per un "locale" booleano (pulsante radio "in" e pulsante radio "out"). Ovviamente puoi anche sovrascrivere altri metodi classici. Penso che forse è una soluzione migliore rispetto all'estensione dell'intera classe di preferenze. Sperando che aiuti qualcuno.

Problemi correlati