J'essaie de créer une application qui calcule le poids des planètes. Dans la première vue, l'utilisateur entre son poids et dispose d'un bouton d'entrée, dans la deuxième vue, il y a une liste de boutons radio et un bouton de sélection. Je veux qu'il multiplie son poids par la force de la planète. Pour ce faire, j'avais besoin de la variable weight à la fois dans activity_main.xml et planets.xml. Au début, je ne l'avais que dans le fichier planets, mais maintenant qu'elle est également dans le fichier main, les instructions if else se trompent.
activité_main.xml :
<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" >
<TextView
android:id="@+id/askwtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="19dp"
android:text="@string/askwt" />
<EditText
android:id="@+id/inputwtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/askwtTextView"
android:layout_below="@+id/askwtTextView"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/inputwtEditText"
android:layout_below="@+id/inputwtEditText"
android:layout_marginTop="38dp"
android:onClick="buttonclick"
android:text="@string/enter" />
</RelativeLayout>
planètes.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/planetTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet" />
<TextView
android:id="@+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/mercuryRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/mercury" />
<RadioButton
android:id="@+id/venusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/venus" />
<RadioButton
android:id="@+id/earthRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/earth" />
<RadioButton
android:id="@+id/marsRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/mars" />
<RadioButton
android:id="@+id/jupiterRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jupiter" />
<RadioButton
android:id="@+id/saturnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saturn" />
<RadioButton
android:id="@+id/uranusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/uranus" />
<RadioButton
android:id="@+id/neptuneRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/neptune" />
<RadioButton
android:id="@+id/plutoRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pluto" />
</RadioGroup>
<Button
android:id="@+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonclick2"
android:text="@string/select" />
<TextView
android:id="@+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
JAVA :
package com.deitel.planetaryweight;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends Activity {
//Global variable
double weight;
private Button enter; // creates a button
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
enter = (Button) findViewById(R.id.enterButton);
//Start with first screen
setContentView(R.layout.activity_main);
}
//buttonclick for form 1
public void buttonclick(View view){
//creates an editext and assigns the resource id of the xml edittext.
EditText wtentry = (EditText)findViewById(R.id.inputwtEditText);
//Receives the input from the edittext, converts it to a double (number).
weight = Double.parseDouble(wtentry.getText().toString());
//switch views to screen 2
setContentView(R.layout.planets);
//change the value of the textview on screen 2 to the calculation value
TextView t2 = (TextView)findViewById(R.id.textViewform2);
t2.setText(Double.toString(weight));
}
//buttonclick for form 2!
public void buttonclick2(View view){
setContentView(R.layout.planets);
RadioButton mercury = (RadioButton) findViewById(R.id.mercuryRadio);
RadioButton venus = (RadioButton) findViewById(R.id.venusRadio);
RadioButton earth = (RadioButton) findViewById(R.id.earthRadio);
RadioButton mars = (RadioButton) findViewById(R.id.marsRadio);
RadioButton jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
RadioButton saturn = (RadioButton) findViewById(R.id.saturnRadio);
RadioButton uranus = (RadioButton) findViewById(R.id.uranusRadio);
RadioButton neptune = (RadioButton) findViewById(R.id.neptuneRadio);
RadioButton pluto = (RadioButton) findViewById(R.id.plutoRadio);
//Makes a variable for the entered amount
Double mercurypf;
Double venuspf;
Double earthpf;
Double marspf;
Double jupiterpf;
Double saturnpf;
Double uranuspf;
Double neptunepf;
Double plutopf;
Double weight;
// constants
final double mercuryforce = 0.38;
final double venusforce = 0.91;
final double earthforce = 1.00;
final double marsforce = 0.38;
final double jupiterforce = 2.34;
final double saturnforce = 1.06;
final double uranusforce = .92;
final double neptuneforce = 1.19;
final double plutoforce = 0.06;
// Code used to determine which planet RadioButton is checked:
if(mercury.isChecked())
{
mercurypf = mercuryforce * weight;
}
else
{
mercurypf = 0.00;
}
if(venus.isChecked())
{
venuspf = venusforce * weight;
}
else
{
venuspf = 0.00;
}
if(earth.isChecked())
{
earthpf = earthforce * weight;
}
else
{
earthpf = 0.00;
}
if(mars.isChecked())
{
marspf = marsforce * weight;
}
else
{
marspf = 0.00;
}
if(jupiter.isChecked())
{
jupiterpf =jupiterforce * weight;
}
else
{
jupiterpf = 0.00;
}
if(saturn.isChecked())
{
saturnpf = saturnforce * weight;
}
else
{
saturnpf = 0.00;
}
if(uranus.isChecked())
{
uranuspf = uranusforce * weight;
}
else
{
uranuspf = 0.00;
}
if(neptune.isChecked())
{
neptunepf = neptuneforce * weight;
}
else
{
neptunepf = 0.00;
}
if(pluto.isChecked())
{
plutopf = plutoforce * weight;
}
else
{
plutopf = 0.00;
}
}
}