android - findviewbyid 在 preferenceactivity 中的使用

标签 android android-preferences preferenceactivity

我有一个首选项屏幕,我正在使用 android:layout 属性指定布局资源。在偏好 Activity 中,我使用 onCreate 方法中的 addPreferencesFromResource 使用/膨胀偏好。在布局内部我有几个 TextView 和按钮。

问题是我无法使用 findViewById 方法找到任何 TextView 、复选框和按钮! findViewById 的每个结果都返回 null。

编辑:

我需要为复选框附加一个监听器。

有人可以帮我吗?

编辑 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) 首选项屏幕

<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)创建时的偏好 Activity :

编辑 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));
}

我也在 onPostCreate 中尝试了上面的代码,但返回的值仍然为空。

我想找到 responder_generic.xml 和 email_preferences.xml 的复选框。有没有办法找到并添加听众?

谢谢, 阿迪提亚

最佳答案

我所做的是创建一个自定义首选项并且它起作用了。我只是扩展了 Preference 类并扩充了 layout/%MY_LAYOUT%.xml 中提供的布局。听众也被召唤,这是我想要实现的。

我不相信这个解决方案,因此,我将其添加到社区 Wiki。

注意:如果添加了单个首选项,则 single preference solution应该足够了。我在屏幕上添加了多个首选项(在我的案例 2 中),我实现了自定义首选项。

关于android - findviewbyid 在 preferenceactivity 中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728192/

相关文章:

android - 单击 DialogFragment 正按钮后如何执行操作

php - 即使在浏览器 Chrome 中执行 php 文件正常,Android 登录时获取的 json 对象为空

android - 错误 : Error: String types not allowed (at 'entries' with value 'array/list' )

android - 如何在 Android 中更改偏好类别的文本颜色?

android - 如何从 PreferenceActivity 创建 Android App Widget 作为配置 Activity

Android:xml文件文件名大写字母及xml文件命名规则

android - 选择打开文件的正确方法

android - Android系统升级后应用偏好是否保留?

Android preference-headers 额外参数

android - 来自 PreferenceManager 的 SharedPreference 默认值与工作 OnSharedPreferenceChangeListener