Android: Comment utiliser les boutons dans l'écran Préférences
Question
Je voudrais fournir un bouton dans le PreferenceActivity. L'utilisateur doit être en mesure de définir un le temps (par exemple via TimePicker), mais il n'y a pas ButtonPreference ou quelque chose comme ça. Je ne veux pas utiliser EditTextPreference.
Alors dois-je utiliser un bouton par défaut dans le PreferenceActivity et enregistrer les paramètres manuellement?
Cordialement,
cody
La solution
Créez un DialogPreference
personnalisé qui intègre un widget TimePicker
. Pas de bouton nécessaire. Devrait être à moins de 100 lignes de code.
Voici un exemple de projet montrant, entre autres, une coutume ColorPreference
.
Autres conseils
Create a different layout including your custom controls and include ListView on top, see the example below
// extra_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView
android:id="@+id/feedback"
android:text="Feedback"
/>
<Button
android:id="@+id/about"
android:text="About"
/>
</LinearLayout>
Once this done, onCreate method of your class which is extended from PreferenceActivity add following line
setContentView(R.layout.settings_extras); //name of your layout
bind OnClickListener
View.OnClickListener feedbackPageRedirect = new View.OnClickListener()
{
public void onClick(View v)
{
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback ");
startActivity(emailIntent);
}
};
Vous pouvez mettre le bouton dans la activity_layout.xml. Par exemple, si « Activity_Setup.java » est l'activité de votre configuration, « activity_setup.xml » est sa mise en page, et « myprefs.xml » est le fichier dans lequel votre mise en page de préférence est stockée, puis
votre devrait mettre le bouton « activity_setup.xml » dans la liste, le plus préférable dans un TableRow, comme ceci:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/llprefs">
<ListView
android:id="@+id/android:list"
android:tag="lvprefs"
android:layout_width="match_parent"
android:layout_height="0.0dp"
android:layout_weight="1" >
</ListView>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<Button
android:id="@+id/btRegister"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/IdonothaveA"
android:onClick="onClick"
/>
<Button
android:id="@+id/btForgot"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/Iforgot"
android:onClick="onClick" />
<!--
<Button
android:id="@+id/btContact"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/Contact"
android:onClick="onClick" />
-->
</TableRow>