我正在尝试创建一个应用程序,使用重量 * 行星力来配置每个行星上某人的行星重量。他们在第一个屏幕上输入自己的体重,然后在下一个屏幕上选择他们想从哪个星球上看到自己的体重。我大部分时间都记下来了,但是当谈到如何在 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>
行星.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());
}
}
我不想要答案和如何一步一步完成任务,我只想从这里得到一些指导。