如何在PreferenceScreen中添加按钮?

Van*_*ren 128 android android-preferences android-button

我对Android开发很新,只是遇到了偏好.我找到了PreferenceScreens并希望用它创建一个登录功能.我唯一的问题是我不知道热,我可以在PreferenceScreen上添加一个"登录"按钮.以下是我的Preference View的样子:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
...
    <PreferenceScreen android:title="@string/login" android:key="Login">
        <EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference>
        <EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference>
    </PreferenceScreen>
...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

Button应位于两个ExitTextPreferences下面.

这个问题有一个简单的解决方案吗?我找到的一个解决方案无法正常工作,因为我使用子偏好屏幕.

更新:

我发现我可以这样添加按钮:

<PreferenceScreen android:title="@string/login" android:key="Login">
        <EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference>
        <EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference>
        <Preference android:layout="@layout/loginButtons" android:key="loginButtons"></Preference>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

并且布局文件(loginButtons.xml)看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:weightSum="10" 
    android:baselineAligned="false" android:orientation="horizontal">
    <Button android:text="Login" android:layout_width="fill_parent"
        android:layout_weight="5" android:layout_height="wrap_content"
        android:id="@+id/loginButton" android:layout_gravity="left"></Button>
    <Button android:text="Password?" android:layout_width="fill_parent"
        android:layout_weight="5" android:layout_height="wrap_content"
        android:id="@+id/forgottenPasswordButton"></Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

所以现在按钮出现但我无法在代码中访问它们.我尝试使用findViewById(),但这返回null.我有什么想法可以访问这些按钮?

Ree*_*eed 295

对于xml:

<Preference android:title="Acts like a button"
                android:key="@string/myCoolButton"
                android:summary="This is a cool button"/>
Run Code Online (Sandbox Code Playgroud)

然后为你的java onCreate()

Preference button = findPreference(getString(R.string.myCoolButton));
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {   
                    //code for what you want it to do   
                    return true;
                }
            });
Run Code Online (Sandbox Code Playgroud)

这看起来像一个普通的偏好,只有一个标题和一个摘要,所以它看起来像它属于.

编辑:我改为使用@string/myCoolButton,getString(R.string.myCoolButton)因为最好使用资源进行编译器帮助,语言翻译和字符串更改的简易性.

  • 许多预加载的谷歌应用程序都使用这种方法,因此最好遵循它!我发现一个标准的灰色按钮在首选项屏幕上看起来不合适,谢谢你,我找到了一个很好的方法让它看起来很标准化:) (7认同)
  • 我刚刚注意到我在2011年的1:11发布了这个.哈哈. (7认同)
  • 确保`android:key ="key"`和`findPreference("key");`使用相同的密钥. (4认同)
  • 实际上这根本不显示按钮,这会使用户感到困惑,而不是OP要求的.正确显示按钮的正确答案是Nicolas的. (3认同)
  • 应该在1个月和11天之后完成:11/1/'11 - 1:11 :) (3认同)
  • 请记住,如果您正在使用PreferenceFragments(API 11及更高版本),请从您的`PreferenceFragment`的`onCreate`调用`findPreference`,而不是'PreferenceActivity` (3认同)

小智 38

我想添加一个退出链接到首选项,并能够修改Jakar的代码,使其工作如下:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >   
   <PreferenceCategory android:title="Settings">
       <Preference android:title="Click to exit" android:key="exitlink"/>       
   </PreferenceCategory>    
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

最初的'偏好'是我编辑过的'EditTextPreference'.

然后在课堂上:

public class MyPreferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.mypreferences);

    Preference button = (Preference)getPreferenceManager().findPreference("exitlink");      
    if (button != null) {
        button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference arg0) {
                finish();   
                return true;
            }
        });     
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 效果很好。那太好了 ! (3认同)

小智 37

我太迟了.这就是我为Button Preference所做的.

在xml文件夹中的preference.xml文件中的首选项

....
    <Preference
        android:key="resetBD"
        android:title="@string/ajustes_almacenamiento"
        android:summary="@string/ajustes_almacenamiento_desc"
        android:widgetLayout="@layout/pref_reset_bd_button" 
    ></Preference>
  ...
Run Code Online (Sandbox Code Playgroud)

和布局文件夹中的pref_reset_bd_button.xml

 <?xml version="1.0" encoding="utf-8"?>
   <Button
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/resetButton" 
        android:text="@string/ajustes_almacenamiento_bt" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="resetearBD">

   </Button>
Run Code Online (Sandbox Code Playgroud)

  • 请告诉我如何与ID"resetButton"感谢找到这个按钮! (3认同)
  • 我发现使用 android:layout 优于 android:widgetLayout。使用 widgetLayout,我的按钮变成了我只能描述为layout_alignParentRight =“true”的东西,但是当使用 android:layout 时,这种行为就消失了 (3认同)

Thi*_*men 7

setContentView(R.layout.buttonLayout);
Run Code Online (Sandbox Code Playgroud)

下面

addPreferencesFromResource(R.xml.yourPreference);
Run Code Online (Sandbox Code Playgroud)

buttonLayout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
        android:id="@+id/top_control_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</RelativeLayout>

<LinearLayout
        android:id="@+id/bottom_control_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:text="@string/saveAlarm"/>
</LinearLayout>

<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_above="@id/bottom_control_bar"
        android:layout_below="@id/top_control_bar"
        android:choiceMode="multipleChoice">
</ListView>
Run Code Online (Sandbox Code Playgroud)

访问按钮:

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //Your event
    }
});
Run Code Online (Sandbox Code Playgroud)

您可以通过将按钮放在RelativeLayout中来获取屏幕顶部或底部的按钮:

  • top_control_bar
  • bottom_control_bar

bottom_control_bar

这对我有用.我希望我能用这段代码帮助别人.


Eri*_*vik 1

我认为没有一种简单的方法可以将按钮添加到首选项屏幕。偏好仅限于:

EditTextPreference
ListPreference
RingtonePreference
CheckboxPreference
Run Code Online (Sandbox Code Playgroud)

使用首选项屏幕时,您只能使用这些选项,并且没有可以轻松满足您需求的单一“按钮首选项”。

我一直在寻找类似的功能,向首选项屏幕添加按钮,但似乎您需要为此构建自己的屏幕,在您自己的实现中管理首选项的更改。