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

发布于 2024-09-28 00:26:43 字数 197 浏览 4 评论 0原文

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

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

问候,

科迪

I'd like to provide a Button in the PreferenceActivity. The user should be able to set a
time (e.g. via TimePicker), but there's no ButtonPreference or something like that. I don't want to use EditTextPreference.

So do I have to use a default Button in the PreferenceActivity and save the settings for it manually?

Regards,

cody

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

围归者 2024-10-05 00:26:43

创建一个包含 TimePicker 小部件的自定义 DialogPreference。无需按钮。代码应少于 100 行。

这是一个示例项目,其中显示了自定义ColorPreference

Create a custom DialogPreference that incorporates a TimePicker widget. No button required. Should be under 100 lines of code.

Here is a sample project showing, among other things, a custom ColorPreference.

二手情话 2024-10-05 00:26:43
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);
            }
        };
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);
            }
        };
梦罢 2024-10-05 00:26:43

您可以将按钮放在 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>

You can put the button in the activity_layout.xml . For example, if "Activity_Setup.java" is your setup activity, "activity_setup.xml" is its layout, and "myprefs.xml" is the file where your preference layout is stored, then

your should put the button in "activity_setup.xml" under the list, most preferable in a TableRow, like this:

<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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文