gisclace Posted March 23, 2015 Share Posted March 23, 2015 (edited) Bonjour, Je suis en train de faire une application simple de calcul. Je me retrouve face à un souci que je n'arrive pas à corriger. J'ai 4 champs qui doivent se multiplier, le tout fonctionne bien, sauf quand je mets un chiffre à virgule (ou un point). Voici les codes : activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" 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.libremagshare.lmssumtwonumbers.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Masse"> </TextView> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longeur"> </TextView> <EditText android:id="@+id/amount1" android:layout_width="76dp" android:layout_height="wrap_content" android:text="" > </EditText> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Largeur" > </TextView> <EditText android:id="@+id/amount3" android:layout_width="103dp" android:layout_height="wrap_content" android:text="" > </EditText> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Epaisseur" > </TextView> <EditText android:id="@+id/amount2" android:layout_width="103dp" android:layout_height="wrap_content" android:text="" > </EditText> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Densité" > </TextView> <EditText android:id="@+id/amount4" android:layout_width="103dp" android:layout_height="wrap_content" android:text="" > </EditText> <Button android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" > </Button> <EditText android:id="@+id/tt" android:layout_width="282dp" android:layout_height="wrap_content" android:text=" " > </EditText> </LinearLayout> Mainactivity.java package gisclace.sum; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; import android.widget.TextView; import android.widget.Button; import android.view.View; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { EditText amount1; EditText amount2; EditText amount3; EditText amount4; TextView tt; Button calculate; int v=0; int w=0; int x=0; int y=0; int z=0; @[member="override"] protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initControls(); } private void initControls() { amount1=(EditText)findViewById(R.id.amount1); amount2=(EditText)findViewById(R.id.amount2); amount3=(EditText)findViewById(R.id.amount3); amount4=(EditText)findViewById(R.id.amount4); tt=(TextView)findViewById(R.id.tt); calculate=(Button)findViewById(R.id.calculate); calculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v) { calculate(); } }); } private void calculate() { v = Integer.parseInt(amount1.getText().toString()); w = Integer.parseInt(amount2.getText().toString()); x = Integer.parseInt(amount3.getText().toString()); y = Integer.parseInt(amount4.getText().toString()); int z=v*w*x*y; tt.setText("La masse est de "+z+"KG."); } @[member="override"] public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @[member="override"] public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } Je ne comprends pas pourquoi cela ne marche pas. Merci d'avance si l'un de vous à la solution. Edited March 23, 2015 by gisclace Quote Link to comment Share on other sites More sharing options...
gisclace Posted March 23, 2015 Author Share Posted March 23, 2015 Bon et bien j'ai finalement résolu le problème. Voici ce que j'ai fait : Pour commencer dans le XML j'ai rajouté : android:inputType="numberDecimal" afin d'afficher seulement les chiffes pour la saisie. J'ai changé : private void calculate() { v = Integer.parseInt(amount1.getText().toString()); w = Integer.parseInt(amount2.getText().toString()); x = Integer.parseInt(amount3.getText().toString()); y = Integer.parseInt(amount4.getText().toString()); int z=v+w+x+y; tt.setText("La masse est de "+z+"KG."); } en : private void calculate() { float v = Float.parseFloat(amount1.getText().toString()); float w = Float.parseFloat(amount2.getText().toString()); float x = Float.parseFloat(amount3.getText().toString()); float y = Float.parseFloat(amount4.getText().toString()); tt.setText(String.valueOf("La masse est de "+v*w*x*y+"Kg")); } Ca fonctionne niquel. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.