单击复选框首选项时更新textview

时间:2018-11-20 06:49:52

标签: textview android-preferences android

我在PreferenceScreen中添加了两个TextViews和一个按钮,并带有很多CheckboxPreference,如下所示。 Picture 当我按下按钮时,我想禁用所有CheckboxPreference并更新metricsChoose textview。但是,setText方法无法做到这一点。

clearAllItem是按钮的onClick方法

public void clearAllItem(View view) {

    metricsChooserTextView.setText("0 metric chosen");
    Toast.makeText(this, "Clear all selected items", Toast.LENGTH_SHORT).show();
}

我的xml文件是这样的

<Preference
        android:selectable="false"
        android:layout="@layout/activity_setting">

    </Preference>

    <PreferenceCategory
        android:key="emotion"
        android:title="Emotion Category">

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="joy"
            android:title="JOY"
            android:summary="Enable or disable detecting joy">

        </CheckBoxPreference>

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="anger"
            android:title="ANGER"
            android:summary="Enable or disable detecting anger">

        </CheckBoxPreference>

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="fear"
            android:title="FEAR"
            android:summary="Enable or disable detecting fear">

        </CheckBoxPreference>

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="disgust"
            android:title="DISGUST"
            android:summary="Enable or disable detecting disgust">

        </CheckBoxPreference> 

activity_setting布局如下:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:textSize="18sp"
        android:text="@string/choose_metrics_message"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/clear_all"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:id="@+id/clear_all_button"
            android:onClick="clearAllItem"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toStartOf="@id/clear_all_button"
            android:textSize="18sp"
            android:id="@+id/metrics_chooser_textview"
            android:text="@string/metric_chooser_default_message"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </ListView>

0 个答案:

没有答案
相关问题