首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在PreferenceScreen中添加按钮?

如何在PreferenceScreen中添加按钮?
EN

Stack Overflow用户
提问于 2011-03-14 20:09:56
回答 8查看 84.6K关注 0票数 146

我是Android开发的新手,只是偶然发现了一些偏好。我找到了PreferenceScreen,并想用它创建一个登录功能。我唯一的问题是,我不知道如何才能添加一个“登录”按钮到PreferenceScreen

下面是我的PreferenceScreen的样子:

代码语言:javascript
复制
<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>

按钮应该在两个EditTextPreference的正下方。

这个问题有没有简单的解决方案?我找到的一个解决方案是不起作用的,因为我使用的是子PreferenceScreen

更新:

我想我可以这样添加按钮:

代码语言:javascript
复制
<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>

布局文件(loginButtons.xml)是这样的:

代码语言:javascript
复制
<?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>

现在按钮出现了,但是我不能在代码中访问它们。我用findViewById()试过了,但这返回了null。你知道我怎么才能访问这些按钮吗?

EN

回答 8

Stack Overflow用户

发布于 2011-08-31 09:11:53

对于xml:

代码语言:javascript
复制
<Preference
    android:title="Acts like a button"
    android:key="@string/myCoolButton"
    android:summary="This is a cool button"
/>

然后,对于onCreate()中的java

代码语言:javascript
复制
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;
    }
});

这将看起来像一个普通的首选项,只有一个标题和一个摘要,所以它看起来像是属于它。

票数 330
EN

Stack Overflow用户

发布于 2015-02-08 05:53:31

我想已经太晚了。这就是我为Button首选项所做的。

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

代码语言:javascript
复制
....
    <Preference
        android:key="resetBD"
        android:title="@string/ajustes_almacenamiento"
        android:summary="@string/ajustes_almacenamiento_desc"
        android:widgetLayout="@layout/pref_reset_bd_button" 
    ></Preference>
  ...

和布局文件夹中的pref_reset_bd_button.xml

代码语言:javascript
复制
 <?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>
票数 46
EN

Stack Overflow用户

发布于 2012-07-27 02:56:13

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

代码语言:javascript
复制
<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>

最初的“Preference”是我手工编辑的“EditTextPreference”。

然后在课堂上:

代码语言:javascript
复制
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;
            }
        });     
    }
}
}
票数 38
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5298370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档