在preferenceactivity findviewbyid的使用 [英] usage of findviewbyid in preferenceactivity
问题描述
我有一个preference屏幕,我使用的是安卓指定一个布局资源:布局
属性。在preference活动我使用/充气使用的preference增加preferencesFromResource
从的onCreate
方法。内部的布局,我有几个textviews和按钮。
I have a preference screen and i am specifying a layout resource using the android:layout
attribute. In the preference activity i am using/inflating the preference using addPreferencesFromResource
from onCreate
method. Inside the layout i have few textviews and buttons.
问题是我无法找到任何使用 findViewById
方法textviews,复选框和按钮!为 findViewById
每个结果返回null。
The problem is i am unable to find any of the textviews, checkboxes and buttons using findViewById
method ! Every result for findViewById
returns null.
修改
我需要一个监听器附加到复选框。
I need to attach a listener to the checkbox.
任何人都可以请帮我在这里?
Can anyone please help me over here ?
编辑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)preference屏幕
<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)preference活动的onCreate:
修改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);
}
和
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));
}
我试着在上面code onPostCreate
以及,但仍返回值为null。
I tried the above code in onPostCreate
as well but still the value returned is null.
我想找到responder_generic.xml和email_ preferences.xml复选框。有没有一种方法查找并添加听众给他们?
I want to find checkboxes for responder_generic.xml and email_preferences.xml. Is there a way to find and add listeners to them ?
谢谢,
Adithya
Thanks, Adithya
推荐答案
我所做的就是创建一个自定义preference和它的工作。我只是延长 preference
类和充气布局/%MY_LAYOUT%的.xml提供的布局。的听众都被称为以及这正是我想达到的。
What i did was create a custom preference and it worked. I simply extended Preference
class and inflated the layout provided in the layout/%MY_LAYOUT%.xml. The listeners were being called as well which is what i wanted to achieve.
该解决方案是无处我相信,所以我加入这个社区维基。
This solution was nowhere i believe, hence, i adding this to community Wiki.
注意:如果有一个单一的preference添加那么的单preference解决方案应该足够了。我加入在屏幕上多个preferences(在我的情况2),我实现了自定义的preferences。
Note : if there is a single preference being added then single preference solution should suffice. I am adding multiple preferences on screen (in my case 2), i implemented custom preferences.
这篇关于在preferenceactivity findviewbyid的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!