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:
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:
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);
}
} |