0

重量*惑星力を使用して、各惑星の誰かの惑星重量を構成するアプリケーションを作成しようとしています。最初の画面で体重を入力し、次の画面で体重を表示する惑星を選択します。私はほとんどそれをダウンさせていますが、Javaで各フォームを構成する方法を理解することになると、私は迷子になります...

私の最初の xml である activity_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:text="@string/enter" />

</RelativeLayout>

Planets.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" />

<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:text="@string/select" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

</LinearLayout>

そしてJAVA、計算*2は、私が必要とするかもしれない別の方程式のプレースホルダーです...私は思う:

package com.deitel.planetaryweight;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.View;
import android.view.Menu;

public class MainActivity extends Activity {

//Global variable
double calculation = 0.0;
private Button select;  // creates a button 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    select = (Button) findViewById(R.id.selectButton);

    //Start with first screen
    setContentView(R.layout.activity_main);
}

//buttonclick for form 1
public void buttonclick(View view){

    TextView t = (TextView) findViewById(R.id.textView1);
     //take the value of the text in the textview and multiply by 2
      calculation = Double.parseDouble(t.getText().toString()) * 2;

      //switch views to screen 2
       setContentView(R.layout.main2);
     //change the value of the textview on screen 2 to the calculation value
       TextView t2 = (TextView)findViewById(R.id.textViewform2);
       t2.setText(Double.toString(calculation));

        }

        //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;

            //Receives the input from the inputwt edittext, converts it to a double  (number).
            weight = Double.parseDouble(inputwt.getText().toString());


        }
    }

答えや、タスクを完了する方法の段階的な説明は必要ありません。ここからどこへ行くべきかについてのガイダンスが欲しいだけです。

4

1 に答える 1

1

質問のタイトルに基づいて、少なくとも 2 つのアクティビティが必要です。ユーザーが最初に体重を入力し、2 番目に惑星を選択します。2 番目の「ビュー」で惑星の重量を表示している場合は、3 番目のビューは必要ありません。

OnClickListenerこのために、既に実装した方法と同様の方法で最初のビューを作成する必要があります。ただし、に対してを設定しますenterButton。重量値が入力され、ユーザーが をタップするenterButtonと、OnClickListenerが作成Intentされ、重量値が追加 ( putExtra) として渡され、2 番目の が開始されActivityます。

1st の Planet.xml レイアウトは必要ありませんActivity

最初のものを完成させる最も簡単な方法Activityは、それを実装することOnClickListenerです。

public class MainActivity extends Activity implements OnClickListener{

onClickクリックを渡すメソッドを実装しenterButtonます。

からonClick、2 番目のアクティビティを開始します (別のメソッドで、またはメソッドで直接実行します。次のonClickようなものです。

    Intent selectPlanetIntent = new Intent(this, PlanetActivity.class);
    selectPlanetIntent.putExtra("UserWeight", userWeight);
    startActivity(selectPlanetIntent);

2 番目のアクティビティでsetContentViewは、惑星のレイアウトに。を使用してユーザーの体重を取得しますgetFloatExtra。何かのようなもの:

    Intent startingIntent = getIntent();
    float userWeight = startingIntent.getFloatExtra("UserWeight", 0f);
于 2012-11-14T18:38:13.083 に答える