seekbar associée à un radiogroup
Bonsoir,
J'ai un souci avec une sekkbar, associée à un radiogroup:
Mon radiogroup à 10 radioboutons, et ma seekbar doit aller de 0 à 10, par pas de 1. Pour info, c'est une app pour noter la douleur selon une échelle dite 'EN' (échelle numérique) de 0 à 10.
Mon layout:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
<RadioGroup
android:id="@+id/Radio_Douleur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:minHeight="0dp"
android:minWidth="0dp"
android:orientation="horizontal"
android:weightSum="0">
<RadioButton
android:id="@+id/defaultSelection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="2"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="3"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="4"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="6"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="7"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="8"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="9"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/custom_radio_button"
android:button="@null"
android:gravity="center"
android:text="10"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</RadioGroup>
<SeekBar
android:id="@+id/Seek_Douleur"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="10"
android:paddingEnd="6dip"
android:paddingStart="6dip"
android:progress="0"
android:progressDrawable="@drawable/progress"
android:thumb="@drawable/curseur"
android:thumbOffset="10dp"/> |
Et mon code:
(je passe les détails)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
public class Nouvelle_Migraine .....
SeekBar seekBar;
RadioGroup rg;
RadioButton selectedRadioButton;
.../...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
.../...
rg = (RadioGroup) findViewById (R.id.Radio_Douleur);
// on met le premier bouton à "true"
((RadioButton) rg.getChildAt (0)).setChecked (true);
// On pose notre listener sur le radio group
setRadioButton();
seekBar = (SeekBar) findViewById (R.id.Seek_Douleur);
// gestion de la seekbarr de la douleur
seekBar.setOnSeekBarChangeListener (this);
.../...
}
// gestion de la seekbar
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// à partir de la on change l'état des radio bouton quand la seekbar change
Log.d ("seekbar", "id= " + progress);
((RadioButton) rg.getChildAt (progress)).setChecked (true);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
// gestion des radio boutons
public void setRadioButton()
{
//Récupérer le Radio Button qui est sélectionné
int selectedId = rg.getCheckedRadioButtonId();
selectedRadioButton = (RadioButton) findViewById(selectedId);
//Listener
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
selectedRadioButton = (RadioButton) findViewById(checkedId);
seekBar.setProgress(checkedId);
Log.d ("radiobouton", "id= " + checkedId);
Toast.makeText(context, String.valueOf(selectedRadioButton.getText().toString()), Toast.LENGTH_SHORT).show();
}
});
} |
A présent, cela marche ... presque!
lorsque je bouge la seekbar, je monte de 1 en 1, la c'est bon, mais dès que je vais sur 0, dans les log, j'ai ceci:
Code:
1 2 3 4 5 6
|
10-19 18:46:09.364 20489-20489/org.suinot.migraine I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
10-19 18:46:09.462 20489-20489/org.suinot.migraine I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
10-19 18:46:09.464 20489-20489/org.suinot.migraine D/seekbar: id= 10
10-19 18:46:09.465 20489-20489/org.suinot.migraine D/radiobouton: id= 10
10-19 18:46:09.471 20489-20489/org.suinot.migraine D/radiobouton: id= 2131427479 |
mon radio bouton ne va pas à 0 et ma seekbar va à 10.
J'avoue ne pas comprendre l'id=2131427479.
Est-ce que les radioboutons peuvent aller de 0 à 10? ou j'ai loupé quelque chose?
Merci de votre aide.
Rémi.