我对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.我有什么想法可以访问这些按钮?
有时这种情况发生在开发时.我会对我的源代码进行更改,点击全部保存,然后运行,但更改显然不会反映在应用程序中 - (我正在使用设备进行测试).我甚至可以在我的设备上卸载应用程序,再次点击运行,新安装的应用程序仍未反映源代码中的更改.当发生这种情况时,我必须编辑源代码,命中运行,然后可能会出现一个新版本,其中包含我预期的更改将在设备上.
我也尝试过这里的解决方案,但它似乎并不常用.