Jestem całkiem nowy w programowaniu na Androida i właśnie natknąłem się na Preferencje. Znalazłem PreferenceScreeni chciałem stworzyć za jego pomocą funkcję logowania. Jedyny problem, jaki mam, to to, że nie wiem, jak dodać przycisk „Zaloguj się” do pliku PreferenceScreen.
Oto jak PreferenceScreenwygląda mój :
<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>
Przycisk powinien znajdować się tuż pod dwoma EditTextPreferenceliterami.
Czy istnieje proste rozwiązanie tego problemu? Jedyne rozwiązanie, które znalazłem, nie działa, ponieważ używam subs PreferenceScreen.
Aktualizacja:
Odkryłem, że mogę dodać przyciski w ten sposób:
<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>
a plik układu ( loginButtons.xml) wygląda następująco:
<?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>
Teraz pojawiają się przyciski, ale nie mogę uzyskać do nich dostępu w kodzie. Próbowałem, findViewById()ale to zwraca wartość null. Jakieś pomysły, jak mogę uzyskać dostęp do tych przycisków?
android:onClick="method"do każdego przycisku, gdzie metoda jest zdefiniowana w działaniu jako public void method(View v).
