问题描述
我是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 EditTextPreference
s.
有没有针对此问题的简单解决方案?我发现的一个解决方案无法正常工作,因为我使用了PreferenceScreen
s.
Is there a simple solution for this problem? The one solution I found was not working because I use sub PreferenceScreen
s.
我发现我可以这样添加按钮:
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;
}
});
这看起来像是普通的首选项,只有标题和摘要,因此看起来像是它的所属.
This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.
我改为使用@string/myCoolButton
和getString(R.string.myCoolButton)
,因为最好是使用资源来提供编译器帮助,语言翻译和轻松地进行String更改.
I changed to using @string/myCoolButton
and getString(R.string.myCoolButton)
because it's best to use resources for compiler assistance, language translation, and ease of String changes.
这篇关于如何在PreferenceScreen中添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!