如何向 PreferenceScreen 添加按钮?
我对 Android 开发很陌生,刚刚遇到了首选项。 我找到了 PreferenceScreen
并想用它创建一个登录功能。我遇到的唯一问题是我不知道如何向 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
的正下方。
对于这个问题有一个简单的解决方案吗?我发现的一个解决方案不起作用,因为我使用了子 PreferenceScreen
。
更新:
我发现我可以通过这种方式添加按钮:
<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
)看起来就是这样:
<?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。我有什么想法可以访问这些按钮吗?
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
.
Here's what my PreferenceScreen
looks like:
<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>
The Button should be right under the two EditTextPreference
s.
Is there a simple solution for this problem? The one solution I found was not working because I use sub PreferenceScreen
s.
Update:
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>
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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于 xml:
然后对于
onCreate()
中的 java这将看起来像一个普通的首选项,只有一个标题和一个摘要,所以它看起来像它所属的。
注释
android:
前缀。答案已更新为app:
前缀。For the xml:
Then for the java in your
onCreate()
This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.
Notes
android:
prefix. Answer is updated withapp:
prefix.我想已经太晚了。
这就是我为按钮首选项所做的事情。
xml文件夹中的preference.xml文件
和layout文件夹中的pref_reset_bd_button.xml中的首选项
I suppouse its too late.
This is what i have done for a Button Preference.
The preference in preference.xml file in xml folder
and pref_reset_bd_button.xml in layout folder
我想向首选项添加退出链接,并能够修改 Jakar 的代码以使其像这样工作:
最初“首选项”是我手工编辑的“EditTextPreference”。
然后在课堂上:
I wanted to add an Exit link to the preferences and was able to modify Jakar's code to make it work like this:
Originally the 'Preference' was a 'EditTextPreference' which I hand edited.
Then in the class:
添加
下面的
按钮布局:
访问按钮的方式:
您可以通过将按钮放在相对布局中来获取屏幕顶部或底部的按钮:
Bottom_control_bar
这对我有用。我希望我可以帮助某人完成这段代码。
Add
Below
buttonLayout:
Access Button by:
You can get the button on top or on bottom of the screen by putting the button in RelativeLayout:
bottom_control_bar
This worked for me. I hope I can help someone with this piece of code.
我认为没有一种简单的方法可以将按钮添加到首选项屏幕。首选项仅限于:
使用首选项屏幕时,您只能使用这些选项,并且没有可以轻松满足您需要的单个“按钮首选项”。
我一直在寻找类似的功能,向首选项屏幕添加按钮,但似乎您需要为此构建自己的屏幕,在您自己的实现中管理首选项的更改。
I don't think there is an easy way to just add a Button to a preference screen. Preferences are limited to:
While using a preference screen, you are limited to the use of these options, and there are no single "button-preference" which could be easily used for your need.
I've been looking for similar functionality, with adding buttons to the preference screen, but it seems like you need to build your own screen for this, managing the change to preferences in your own implementation.
您可以像这样访问按钮!
不要忘记将
android:id
添加到包含 layoutButtons.xml 中按钮的 LinearLayout,即android:id="@+id/mylayout"
You can do it like this to access the button!
Don't forget to add
android:id
to the LinearLayout that contains the button in layoutButtons.xml, i.e.android:id="@+id/mylayout"
您还可以通过重写
Preference.onCreateView(parent)
来自定义 Preference 的布局。下面的示例使用匿名内部类来进行红色首选项。您可以使用此技术将按钮添加到默认视图。
You can also customise the layout of a Preference by overriding
Preference.onCreateView(parent)
. The example below uses an anonymous inner class to make red preferences.You could use this technique to add a button to the default view.
为
SettingsFragment.java
创建自定义布局,即layout_settings.xml
然后,引用
styles.xml
中的布局文件:然后,在
SettingsFragment.java
的onViewCreated()
方法中,您可以使用这样的按钮:
Create a custom layout for the
SettingsFragment.java
i.elayout_settings.xml
Then, refer to the layout file in your
styles.xml
:And then, in the
onViewCreated()
method of yourSettingsFragment.java
, youcan use the button like this:
如果您想捕获用户点击其中一个 Preferences 项并且您的应用正在使用
PreferenceFragmentCompat
,那么您可以执行以下操作:Java:
Kotlin:
If you'd like to catch a user click on one of the Preferences items and your app is using
PreferenceFragmentCompat
, then you can do this:Java:
Kotlin:
尝试 android:onClick="myMethod" 对于简单的 onclick 事件来说就像一个魅力
try android:onClick="myMethod" works like a charm for simple onclick events