gpt4 book ai didi

java - 我在多个 View 中需要相同的变量,但它不起作用

转载 作者:太空狗 更新时间:2023-10-29 16:06:25 26 4
gpt4 key购买 nike

我正在尝试创建一个计算行星重量的应用程序。在第一个 View 中,用户输入他们的体重并且它有一个输入按钮,在第二个 View 中是一个单选按钮列表和选择按钮。我想让它把它们的重量乘以地球的力。为此,我需要 activity_main.xml 和 planets.xml 的权重变量。起初我只在 planets 中使用它,但现在它也在 main 中,if else 语句出错。

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:onClick="buttonclick"
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" />

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

}
}

最佳答案

一种方法是:

创建一个名为 say Constants 的新类,并在其中将此变量声明为静态变量。现在您可以从所有 Activity 中访问此变量。

public class Constants {
public static double weight;
}

从您的所有 Activity 中访问它

Constants.weight = 2.5;

关于java - 我在多个 View 中需要相同的变量,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13402403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com