如何将按钮添加到 PreferenceScreen? [英] How to add a button to a PreferenceScreen?

查看:50
本文介绍了如何将按钮添加到 PreferenceScreen?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 开发很陌生,刚接触到首选项.我找到了 PreferenceScreen 并想用它创建一个登录功能.我唯一的问题是我不知道如何添加登录"按钮到 PreferenceScreen.

I'm quite new to Android Development and just came across Preferences. I found PreferenceScreen and wanted to create a login functionality with it. The only problem I have is that I don't know how I could add a "Login" button to the PreferenceScreen.

这是我的 PreferenceScreen 的样子:

<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 的正下方.

The Button should be right under the two EditTextPreferences.

这个问题有简单的解决方案吗?我发现的一个解决方案不起作用,因为我使用子 PreferenceScreens.

Is there a simple solution for this problem? The one solution I found was not working because I use sub PreferenceScreens.

我发现我可以通过这种方式添加按钮:

I figured out that i can add buttons this way:

<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) 看起来是这样的:

and the layout file (loginButtons.xml) looks that way:

<?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.知道如何访问这些按钮吗?

So now the buttons appear but I can't access them in code. I tried it with findViewById() but this is returning null. Any ideas how I could access these buttons?

推荐答案

对于 xml:

<Preference
    android:title="Acts like a button"
    android:key="@string/myCoolButton"
    android:summary="This is a cool button"
/>

然后对于 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;
    }
});

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

This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.

这篇关于如何将按钮添加到 PreferenceScreen?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆