IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Impossible de modifier mon NumberPicker


Sujet :

Android

  1. #1
    Membre à l'essai
    Homme Profil pro
    Retraité
    Inscrit en
    Août 2013
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2013
    Messages : 52
    Points : 20
    Points
    20
    Par défaut Impossible de modifier mon NumberPicker
    Salut à tous
    Je suis à tester un NumberPicker horizontal qui fonctionne très bien.
    J'ai tenté ensuite de remplacer le ViewText par un EditText afin de pouvoir entrer un nombre directement sans avoir à utiliser les boutons.
    Je ne trouve pas la cause de mon erreur qui fait que je ne peux pas modifier le nombre.
    Quelqu'un peut me donner un coup de main?
    Merci

    Voici mon fichier activity_main.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
    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
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gris"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.mario.numberpicker.MainActivity" >
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:background="@color/olive" >
     
        <Button
            android:id="@+id/button2"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/moins"
            android:minHeight="20dp"
            android:minWidth="20dp"
            android:layout_marginRight="2dp" />
     
        <EditText
            android:id="@+id/editText2"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:focusable="false"
            android:gravity="left|center"
            android:inputType="number"
            android:text="1"
            android:textColor="@color/blanc"
            android:textSize="20sp" />
     
        <Button
            android:id="@+id/button1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="2dp"
            android:background="@drawable/plus" />
     
        </LinearLayout>
     
    </RelativeLayout>
    Voici mon 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
    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
    package com.mario.numberpicker;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
     
     
    public class MainActivity extends Activity implements OnClickListener {
    	 // Définition des variables globales 
    	 Button btnUp, btnDown;	// Boutons du NumberPicker
    	 EditText editTextMid;  // Champs des chiffres du NumberPicker
    	 int nStart = 1;		// Nombre minimum du NumberPicker
    	 int nEnd = 99;		// Nombre maximum du NumberPicker
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
     
    		  // On récupère toutes les vues dont on a besoin
    		  btnUp = (Button) findViewById(R.id.button1);
    		  btnDown = (Button) findViewById(R.id.button2);
    		  editTextMid = (EditText) findViewById(R.id.editText2);
     
    		  // On affiche les valeurs de base
    		  editTextMid.setText("1");
     
    		  // On attribut un écouteur d'évènement à tout les boutons et champs de saisies
    		  btnUp.setOnClickListener(this);
    		  btnDown.setOnClickListener(this);
    		  editTextMid.addTextChangedListener(textWatcher);
     
     
    	}
     
    	// --- LES MÉTHODES ---
     
    	// Pour Afficher la valeur du résultat final
    	private TextWatcher textWatcher = new TextWatcher() {
    		@Override
    		public void onTextChanged(CharSequence s, int start, int before, int count) {
    		}				
    		@Override
    		public void beforeTextChanged(CharSequence s, int start, int count, int after) {		  
    		}		  
    		@Override
    		public void afterTextChanged(Editable s) {		  
    		}
    	};
     
    	@Override
    	 public void onClick(View v) {
    	  String getString = String.valueOf(editTextMid.getText());
    	  int curent = Integer.parseInt(getString);
    	  if (v == btnUp) {
    	   if (curent < nEnd) {
    	    curent++;
    	    editTextMid.setText(String.valueOf(curent));
    	   }
     
    	  }
    	  if (v == btnDown) {
    	   if (curent > nStart) {
    	    curent--;
    	    editTextMid.setText(String.valueOf(curent));
    	   }
    	  }
    	 }
     
     
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
     
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// Handle action bar item clicks here. The action bar will
    		// automatically handle clicks on the Home/Up button, so long
    		// as you specify a parent activity in AndroidManifest.xml.
    		int id = item.getItemId();
    		if (id == R.id.action_settings) {
    			return true;
    		}
    		return super.onOptionsItemSelected(item);
    	}
    }

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2013
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2013
    Messages : 20
    Points : 31
    Points
    31
    Par défaut
    Bonjour,

    Que cherches tu à faire avec le TextWatcher ?

    Pour information, ceci est ce que l'on appel communément une inner class et c'est franchement moche
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    private TextWatcher textWatcher = new TextWatcher() {
    		@Override
    		public void onTextChanged(CharSequence s, int start, int before, int count) {
    		}				
    		@Override
    		public void beforeTextChanged(CharSequence s, int start, int count, int after) {		  
    		}		  
    		@Override
    		public void afterTextChanged(Editable s) {		  
    		}
    	};
    Sinon je ne vois pas pourquoi il y aurait une erreur.

    Cela dit, dans ta méthode OnClick je te conseille :

    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
    @Override
    public void onClick(View v) 
    {
    	  String getString = String.valueOf(editTextMid.getText());
    	  int curent = Integer.parseInt(getString);
    	  if (v.equals(btnUp))
              {
    	   if (curent < nEnd) 
               {
    	    curent++;
    	    editTextMid.setText(String.valueOf(curent));
    	   }
     
    	  }
              else if (v.equals(btnDown)) 
              {
    	   if (curent > nStart) 
               {
    	    curent--;
    	    editTextMid.setText(String.valueOf(curent));
    	   }
    	  }
    }
    Ce qui ne change en rien la fonctionnalité de ton code bien sur. Et la façon que tu as utilisé est bonne.

  3. #3
    Invité
    Invité(e)
    Par défaut
    Bonjour

    Pour le OnClick, ne serait-il pas plus approprié d'utiliser la méthode getId() de la vue? Ceci dis cela fonctionne très bien en vérifiant si l'instance de "v" est la même que btnUp ou btnDown.
    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
    Override
    public void onClick(View v) 
    {
    	  String getString = String.valueOf(editTextMid.getText());
    	  int curent = Integer.parseInt(getString);
    	  if (v.getId() == btnUp.getId())
              {
    	   if (curent < nEnd) 
               {
    	    curent++;
    	    editTextMid.setText(String.valueOf(curent));
    	   }
     
    	  }
              else if (v.getId() == btnDown.getId()) 
              {
    	   if (curent > nStart) 
               {
    	    curent--;
    	    editTextMid.setText(String.valueOf(curent));
    	   }
    	  }
    }
    le nombre ne peut être changé "au clavier" éventuellement parce que:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <EditText
            android:id="@+id/editText2"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:focusable="false"
            android:gravity="left|center"
            android:inputType="number"
            android:text="1"
            android:textColor="@color/blanc"
            android:textSize="20sp" />
    Du coup tu ne peux pas avoir le focus dans le edittext ? Essaye, c'est peut être ça la cause de ton problème (fait un check pour ne pas caster "je suis toto" en int peut être sinon tu risque de lever une exception).

    J'espère avoir été utile. a+

  4. #4
    Membre à l'essai
    Homme Profil pro
    Retraité
    Inscrit en
    Août 2013
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2013
    Messages : 52
    Points : 20
    Points
    20
    Par défaut
    Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 23
    Dernier message: 02/06/2015, 08h59
  2. Impossible de modifier mon script python
    Par roadbecri dans le forum Général Python
    Réponses: 3
    Dernier message: 22/01/2015, 09h46
  3. Impossible de modifier l'icône 256x256 de mon appli
    Par coincoin73 dans le forum MFC
    Réponses: 3
    Dernier message: 30/10/2012, 10h19
  4. Réponses: 13
    Dernier message: 01/02/2010, 18h56
  5. Impossible de quitter mon application!
    Par Bouillou dans le forum C++Builder
    Réponses: 4
    Dernier message: 12/11/2002, 20h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo