如何将 Spinner 值与之前计算的答案一起添加到新计算中?
How to add a Spinner value along with previous calculated answers into a new calculation?
所以这是我第一个没有遵循教程的应用程序。我已将 xml 全部布置好,微调器下拉菜单也开始工作,这是由 EditText 数字完成的第一个计算工作。
我现在想使用这个第一个计算出的答案以及一个微调器选择的值来进行下一次计算
这是我的 xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/inside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Inside Ø \n(mm)"
android:textSize="16dp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/outside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Outside Ø \n(mm)"
android:textSize="16dp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/coils"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Number of Coils"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<Spinner
android:id="@+id/thickness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
<Spinner
android:id="@+id/width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<Button
android:id="@+id/bt_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="calculate"
android:text="Calculate" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="24dp">
<TextView
android:id="@+id/result_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp">
<TextView
android:id="@+id/result_kg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp">
<TextView
android:id="@+id/result_m2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:layout_gravity="center_horizontal"
android:onClick="clear"
android:text="Clear" />
这是代码
package com.example.android.engineerstoolbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import static com.example.android.engineerstoolbox.R.id.result_kg;
import static com.example.android.engineerstoolbox.R.id.result_m;
import static com.example.android.engineerstoolbox.R.id.result_m2;
public class CoilCalculator extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private EditText editText3;
private Spinner spinner1;
private Spinner spinner2;
private TextView resultM;
private TextView resultKG;
private TextView resultM2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coil_calculator);
//set spinner1 for user input of available thickness
Spinner dropdown = (Spinner) findViewById(R.id.thickness);
String[] items = new String[]{"Thickness", "1.2", "1.5", "2", "2.5", "3", "4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
//set spinner2 for user input of available width
dropdown = (Spinner) findViewById(R.id.width);
items = new String[]{"Width", "1219", "1500", "1524"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
editText1 = (EditText) findViewById(R.id.inside);
editText2 = (EditText) findViewById(R.id.outside);
editText3 = (EditText) findViewById(R.id.coils);
resultM = (TextView) findViewById(result_m);
resultKG = (TextView) findViewById(result_kg);
resultM2 = (TextView) findViewById(result_m2);
//Set button to do calculations
Button bt_calculate = (Button) findViewById(R.id.bt_calculate);
bt_calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeCalculations();
}
});
//Set reset button to reset inputs
Button mButtonReset = (Button) findViewById(R.id.clear);
mButtonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText1.setText("");
editText2.setText("");
editText3.setText("");
resultM.setText("");
}
});
}
private void makeCalculations() {
double n1 = Double.valueOf(editText1.getText().toString());
double n2 = Double.valueOf(editText2.getText().toString());
double n3 = Double.valueOf(editText3.getText().toString());
//do the Lineal meter calculation
Double calculatedValue = ((((n1 + n2) / 2) * 3.141592654) * n3) / 1000;
double output = (double) Math.round(calculatedValue * 1000) / 1000;
//set the value to the textview, to display on screen.
resultM.setText(output + " Lineal Meters");
}
}
我知道我需要设置某种侦听器来获取微调器值;但我不确定在哪里,到目前为止我还没有在这里找到解释代码放置位置的问题。
下一个计算看起来像这样
Double calculatedValue2 = output * (value of spinner2/1000);
//set the value to the textview, to display on screen.
resultM2.setText(calculatedValue2 + " M²");
我很确定双倍不是要走的路,但想为下一次计算展示一些东西
您实际上并未将 spinner1 和 spinner2 分配给 Spinner 对象。
//set spinner1 for user input of available thickness
spinner1 = (Spinner) findViewById(R.id.thickness);
String[] items = new String[]{"Thickness", "1.2", "1.5", "2", "2.5", "3", "4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
spinner1.setAdapter(adapter);
//set spinner2 for user input of available width
spinner2 = (Spinner) findViewById(R.id.width);
items = new String[]{"Width", "1219", "1500", "1524"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
spinner2.setAdapter(adapter);
然后当你想进行计算时,
String selectedItem = spinner2.getSelectedItem().toString();
double selectedValue = Double.parseDouble(selectedItem);
Double calculatedValue2 = output * (selectedValue/1000);
//set the value to the textview, to display on screen.
resultM2.setText(calculatedValue2 + " M²");
如果您想在从微调器中选择一个值时调用侦听器,您应该使用 Spinner.setOnItemSelectedListener 方法。您可以查看此 post 以获取更多信息。
setOnItemSelectedListener of Spinner does not call
您只需覆盖 onItemSelected 方法即可。
所以这是我第一个没有遵循教程的应用程序。我已将 xml 全部布置好,微调器下拉菜单也开始工作,这是由 EditText 数字完成的第一个计算工作。 我现在想使用这个第一个计算出的答案以及一个微调器选择的值来进行下一次计算 这是我的 xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/inside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Inside Ø \n(mm)"
android:textSize="16dp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/outside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Outside Ø \n(mm)"
android:textSize="16dp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/coils"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Number of Coils"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<Spinner
android:id="@+id/thickness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
<Spinner
android:id="@+id/width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<Button
android:id="@+id/bt_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="calculate"
android:text="Calculate" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="24dp">
<TextView
android:id="@+id/result_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp">
<TextView
android:id="@+id/result_kg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp">
<TextView
android:id="@+id/result_m2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:layout_gravity="center_horizontal"
android:onClick="clear"
android:text="Clear" />
这是代码
package com.example.android.engineerstoolbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import static com.example.android.engineerstoolbox.R.id.result_kg;
import static com.example.android.engineerstoolbox.R.id.result_m;
import static com.example.android.engineerstoolbox.R.id.result_m2;
public class CoilCalculator extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private EditText editText3;
private Spinner spinner1;
private Spinner spinner2;
private TextView resultM;
private TextView resultKG;
private TextView resultM2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coil_calculator);
//set spinner1 for user input of available thickness
Spinner dropdown = (Spinner) findViewById(R.id.thickness);
String[] items = new String[]{"Thickness", "1.2", "1.5", "2", "2.5", "3", "4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
//set spinner2 for user input of available width
dropdown = (Spinner) findViewById(R.id.width);
items = new String[]{"Width", "1219", "1500", "1524"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
editText1 = (EditText) findViewById(R.id.inside);
editText2 = (EditText) findViewById(R.id.outside);
editText3 = (EditText) findViewById(R.id.coils);
resultM = (TextView) findViewById(result_m);
resultKG = (TextView) findViewById(result_kg);
resultM2 = (TextView) findViewById(result_m2);
//Set button to do calculations
Button bt_calculate = (Button) findViewById(R.id.bt_calculate);
bt_calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeCalculations();
}
});
//Set reset button to reset inputs
Button mButtonReset = (Button) findViewById(R.id.clear);
mButtonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText1.setText("");
editText2.setText("");
editText3.setText("");
resultM.setText("");
}
});
}
private void makeCalculations() {
double n1 = Double.valueOf(editText1.getText().toString());
double n2 = Double.valueOf(editText2.getText().toString());
double n3 = Double.valueOf(editText3.getText().toString());
//do the Lineal meter calculation
Double calculatedValue = ((((n1 + n2) / 2) * 3.141592654) * n3) / 1000;
double output = (double) Math.round(calculatedValue * 1000) / 1000;
//set the value to the textview, to display on screen.
resultM.setText(output + " Lineal Meters");
}
}
我知道我需要设置某种侦听器来获取微调器值;但我不确定在哪里,到目前为止我还没有在这里找到解释代码放置位置的问题。
下一个计算看起来像这样
Double calculatedValue2 = output * (value of spinner2/1000);
//set the value to the textview, to display on screen.
resultM2.setText(calculatedValue2 + " M²");
我很确定双倍不是要走的路,但想为下一次计算展示一些东西
您实际上并未将 spinner1 和 spinner2 分配给 Spinner 对象。
//set spinner1 for user input of available thickness
spinner1 = (Spinner) findViewById(R.id.thickness);
String[] items = new String[]{"Thickness", "1.2", "1.5", "2", "2.5", "3", "4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
spinner1.setAdapter(adapter);
//set spinner2 for user input of available width
spinner2 = (Spinner) findViewById(R.id.width);
items = new String[]{"Width", "1219", "1500", "1524"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
spinner2.setAdapter(adapter);
然后当你想进行计算时,
String selectedItem = spinner2.getSelectedItem().toString();
double selectedValue = Double.parseDouble(selectedItem);
Double calculatedValue2 = output * (selectedValue/1000);
//set the value to the textview, to display on screen.
resultM2.setText(calculatedValue2 + " M²");
如果您想在从微调器中选择一个值时调用侦听器,您应该使用 Spinner.setOnItemSelectedListener 方法。您可以查看此 post 以获取更多信息。
setOnItemSelectedListener of Spinner does not call
您只需覆盖 onItemSelected 方法即可。