我是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。你知道我怎么才能访问这些按钮吗?
发布于 2011-08-31 09:11:53
对于xml:
<Preference
android:title="Acts like a button"
android:key="@string/myCoolButton"
android:summary="This is a cool button"
/>
然后,对于onCreate()
中的java
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;
}
});
这将看起来像一个普通的首选项,只有一个标题和一个摘要,所以它看起来像是属于它。
发布于 2015-02-08 05:53:31
我想已经太晚了。这就是我为Button首选项所做的。
xml文件夹中preference.xml文件中的首选项
....
<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
<?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>
发布于 2012-07-27 02:56:13
我想向首选项添加一个退出链接,并能够修改Jakar的代码,使其工作方式如下:
<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”。
然后在课堂上:
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;
}
});
}
}
}
https://stackoverflow.com/questions/5298370
复制相似问题