Android:如何在首选项屏幕中使用按钮

时间:2010-10-20 15:32:26

标签: android android-preferences sharedpreferences preferenceactivity preferencescreen

我想在PreferenceActivity中提供一个Button。用户应该能够设置一个 时间(例如通过TimePicker),但没有ButtonPreference或类似的东西。我不想使用EditTextPreference。

那么我必须在PreferenceActivity中使用默认按钮并手动保存它的设置吗?

此致

科迪

3 个答案:

答案 0 :(得分:4)

创建包含DialogPreference小部件的自定义TimePicker。无需按钮。应该在100行代码之下。

Here is a sample project显示自定义ColorPreference

答案 1 :(得分:0)

Create a different layout including your custom controls and include ListView on top, see the example below

// extra_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
    <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" android:layout_weight="1"/>
    <TextView
            android:id="@+id/feedback"
            android:text="Feedback"
            />
    <Button
            android:id="@+id/about"
            android:text="About"
            />
</LinearLayout>

Once this done, onCreate method of your class which is extended from PreferenceActivity add following line

setContentView(R.layout.settings_extras); //name of your layout


bind OnClickListener

View.OnClickListener feedbackPageRedirect = new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback  ");

                startActivity(emailIntent);
            }
        };

答案 2 :(得分:0)

您可以将该按钮放在activity_layout.xml中。例如,如果“Activity_Setup.java”是您的设置活动,则“activity_setup.xml”是其布局,“myprefs.xml”是存储您的首选项布局的文件,然后

你应该将按钮放在列表下的“activity_setup.xml”中,最好是在TableRow中,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/llprefs">

<ListView
    android:id="@+id/android:list"
    android:tag="lvprefs"
    android:layout_width="match_parent"
    android:layout_height="0.0dp"
    android:layout_weight="1" >
</ListView>

    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
     >

    <Button
        android:id="@+id/btRegister"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="@string/IdonothaveA"
        android:onClick="onClick"
         />

    <Button
        android:id="@+id/btForgot"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="@string/Iforgot"
        android:onClick="onClick" />
   <!--  
    <Button
        android:id="@+id/btContact"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="@string/Contact"
        android:onClick="onClick" />
     -->

</TableRow>