Bonjour
Je voudrais utiliser un NumberPicker pour choisir une valeur de -120 à +120. Pas de problème pour faire de 0 à 120, mais dès que j'initialise mon NumberPicker avec un int négatif, la compil se passe bien mais l'appli crashe.
Une idée ?

Fichier main_activity.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/numberPicker"
        android:layout_gravity="center"
        android:layout_weight="1"
        />
 
    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:gravity="center_horizontal"
        android:layout_weight="1"
        android:textSize="36sp" />
 
</LinearLayout>
Fichier MainActivity.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
package net.xxxxx.prog.numberpickerapp;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity{
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        final TextView tv=(TextView)findViewById(R.id.textView);
        NumberPicker np=(NumberPicker)findViewById(R.id.numberPicker);
        np.setMaxValue(10);
        np.setMinValue(-1);
        np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                tv.setText("Numero choisi : "+newVal);
            }
        });
    }
 
}