【问题标题】:Android: How to use Buttons in Preference ScreenAndroid:如何在首选项屏幕中使用按钮
【发布时间】:2011-04-28 03:11:06
【问题描述】:

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

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

问候,

科迪

【问题讨论】:

    标签: android android-preferences sharedpreferences preferenceactivity preferencescreen


    【解决方案1】:

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

    Here is a sample project 显示自定义 ColorPreference 等内容。

    【讨论】:

      【解决方案2】:
      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);
                  }
              };
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 这个问题已经很老了——也可以考虑查看一些更新的问题。欢迎使用 Stack Overflow!
        猜你喜欢
        • 1970-01-01
        • 2011-07-31
        • 2023-03-14
        • 2013-09-25
        • 2013-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-01
        相关资源
        最近更新 更多