计算长度、面积和温度的两个spinner的乘法问题
Posted
技术标签:
【中文标题】计算长度、面积和温度的两个spinner的乘法问题【英文标题】:Problem of multiply two spinners in calculation length,area and temperature 【发布时间】:2021-03-26 15:59:21 【问题描述】:我遇到的问题是应用程序无法执行我设计的任务。 我成功运行了该应用程序,但是当我单击任何微调器时,列表根本不会下拉。没有一个微调器根本没有响应,我不知道问题所在。 我已经尝试了一切来解决这个问题,但我做不到。 我多次运行代码我没有收到错误消息,但微调器仍然不起作用。 请问有人可以帮忙吗? 这个应用程序是假设转换长度,面积和温度。
String.xml 文件
<resources>
<string name="spinner_1">From</string>
<string name="spinner_2">To</string>
<string-array name="conversion_type">
<item>Length</item>
<item>Area</item>
<item>Temperature</item>
</string-array>
<string-array name="area">
<item>Square kilometre</item>
<item>Square metre</item>
<item>Square yard</item>
<item>Square foot</item>
<item>Square inch</item>
<item>Square mile</item>
<item>Hectare</item>
<item>Acre</item>
</string-array>
<string-array name="length">
<item>Kilometre</item>
<item>Metre</item>
<item>Centimetre</item>
<item>Millimetre</item>
<item>Micrometer</item>
<item>Mile</item>
<item>Yard</item>
<item>Foot</item>
<item>Inch</item>
<item>Nautical Mile</item>
</string-array>
<string-array name="temperature">
<item>Celsius</item>
<item>Kelvin</item>
<item>Fahrenheit</item>
</string-array>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<TextView
android:layout_
android:layout_
android:text="Conversion Type"
android:layout_marginTop="10dp"
android:layout_marginLeft="12dp"
android:textSize="16sp"
android:textColor="#607D8B"
android:textStyle="bold"
android:id="@+id/text"/>
<LinearLayout
android:layout_below="@+id/text"
android:layout_
android:layout_
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:id="@+id/first_linear"
android:layout_marginRight="8dp">
<TextView
android:layout_
android:layout_
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:background="#cbcbcb"/>
<Spinner
android:id="@+id/conversion_type"
android:layout_weight="1"
android:layout_
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_ />
<TextView
android:layout_
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_
android:id="@+id/text_some"
android:background="#cbcbcb"/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_below="@id/first_linear"
android:layout_
android:layout_marginRight="8dp"
android:id="@+id/second_linear"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:layout_
android:layout_
android:id="@+id/text_from"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:text="From"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#607D8B"
android:layout_below="@id/first_linear"/>
<Spinner
android:id="@+id/from_spinner"
android:layout_weight="1"
android:layout_
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="20dp"
android:layout_ />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_>
<EditText
android:hint="edit_text_from"
android:id="@+id/from_edit"
android:layout_
android:layout_weight="3"
android:layout_
android:layout_marginTop="15dp"
android:inputType="numberSigned|numberDecimal"
android:layout_marginLeft="30dp" />
<Button
android:layout_
android:layout_
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="Clear"
android:layout_marginBottom="2dp"
android:id="@+id/clear_text_unit"
android:onClick="clear_unit"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_
android:layout_
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="30dp"
android:id="@+id/third_text"
android:layout_below="@+id/second_linear"
android:layout_marginBottom="0dp"
android:background="#FFCBCBCB"/>
<LinearLayout
android:layout_
android:layout_
android:layout_below="@id/third_text"
android:id="@+id/fourth_linear"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:layout_
android:layout_
android:id="@+id/text_to"
android:layout_marginTop="20dp"
android:layout_marginLeft="6dp"
android:text="To"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#607D8B" />
<Spinner
android:id="@+id/to_spinner"
android:layout_weight="1"
android:layout_
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:layout_ />
</LinearLayout>
<TextView
android:layout_
android:layout_
android:layout_marginLeft="33dp"
android:background="#e6e5e5"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:maxLines="1"
android:scrollHorizontally="true"
android:layout_marginTop="20dp"
android:id="@+id/to_text"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginRight="30dp"/>
</LinearLayout>
<TextView
android:layout_
android:layout_
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="40dp"
android:layout_below="@+id/fourth_linear"
android:layout_marginBottom="10dp"
android:background="#FFCBCBCB"/>
</RelativeLayout>
MainActivity.java 文件
package com.example.learnspinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity
int spinner_cycle_pos = 0, spinner_duration_pos = 0;
Spinner from_spinner, to_spinner, conversion_type, duration_spinner, cycle_spinner;
String from_spinner_string, to_spinner_string, choice_spinner;
int from_spinner_position, to_spinner_position, choice_spinner_pos;
Button btnConvert;
TextView result;
EditText quantity;
ArrayAdapter<CharSequence> adapter_length, adapter_temperature, adapter_area;
BigDecimal[][] array = new BigDecimal("1"), new BigDecimal("1000000.0"), new BigDecimal("1195990.05"), new BigDecimal("10763910.4"), new BigDecimal("1550000000"), new BigDecimal("0.386102"), new BigDecimal("100"), new BigDecimal("247.105"),
new BigDecimal("1"), new BigDecimal("0.125"), new BigDecimal("0.000122073125"), new BigDecimal("0.00000011920992"), new BigDecimal("0.0000000001164153"), new BigDecimal("0.0000000000001136868377"), new BigDecimal("0.000000000000001110223024"),
new BigDecimal("1"), new BigDecimal("0.001"), new BigDecimal("0.000239006"), new BigDecimal("0.00000027778"), new BigDecimal("0.737562"),
new BigDecimal("1"), new BigDecimal("0.001"), new BigDecimal("0.000001"), new BigDecimal("0.000000001"),
new BigDecimal("1"), new BigDecimal("1000"), new BigDecimal("1000000"), new BigDecimal("1000000000"), new BigDecimal("1000000000000"), new BigDecimal("157.473"), new BigDecimal("2204.62"), new BigDecimal("35274"),
new BigDecimal("1"), new BigDecimal("1000"), new BigDecimal("100000"), new BigDecimal("10000000000"), new BigDecimal("10000000000000"), new BigDecimal("0.621371"), new BigDecimal("1093.61"), new BigDecimal("3280.84"), new BigDecimal("39370.1"), new BigDecimal("0.539953"),
new BigDecimal("1"), new BigDecimal("1.1111111300619"), new BigDecimal("0.0174533"), new BigDecimal("17.453300"),
new BigDecimal("1"), new BigDecimal("1.01325"), new BigDecimal("101325"), new BigDecimal("760"),
new BigDecimal("1"), new BigDecimal("1.46667"), new BigDecimal("0.44704"), new BigDecimal("1.60934"), new BigDecimal("0.868976"),
, // keep blank for temperature
new BigDecimal("1"), new BigDecimal("0.001"), new BigDecimal("0.000016667"), new BigDecimal("0.00000027778"), new BigDecimal("0.000000011574"), new BigDecimal("0.0000000016534"), new BigDecimal("0.00000000038052"), new BigDecimal("0.00000000003171"), new BigDecimal("0.000000000003171"), new BigDecimal("0.0000000000003171"),
new BigDecimal("1"), new BigDecimal("1000"), new BigDecimal("0.219969"), new BigDecimal("0.879877"), new BigDecimal("3.51951"), new BigDecimal("0.0353147"), new BigDecimal("61.0237"), new BigDecimal("61023.7");
String val = "0";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private void initialise_adapters()
adapter_temperature = ArrayAdapter.createFromResource(getApplication(), R.array.temperature, android.R.layout.simple_spinner_item);
adapter_length = ArrayAdapter.createFromResource(getApplication(), R.array.length, android.R.layout.simple_spinner_item);
adapter_area = ArrayAdapter.createFromResource(getApplication(), R.array.area, android.R.layout.simple_spinner_item);
private void unit_converter()
from_spinner = findViewById(R.id.from_spinner);
to_spinner = findViewById(R.id.to_spinner);
result = findViewById(R.id.to_text);
quantity = findViewById(R.id.from_edit);
conversion_type = (Spinner) findViewById(R.id.conversion_type);
// Initialise the main spinner
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplication(), R.array.conversion_type, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
conversion_type.setAdapter(adapter);
conversion_type.setOnItemSelectedListener(new spinner());
from_spinner = (Spinner) findViewById(R.id.from_spinner);
to_spinner = (Spinner) findViewById(R.id.to_spinner);
initialise_adapters();
//initally set default to area adapter
adapter_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_area);
to_spinner.setAdapter(adapter_area);
from_spinner_string = "Square kilometer";
to_spinner_string = "Square kilometer";
choice_spinner = "Area";
//Default values
from_spinner_position = 0;
to_spinner_position = 0;
choice_spinner_pos = 0;
int spinner_cycle_pos = 0, spinner_duration_pos = 0;
from_spinner.setOnItemSelectedListener(new spinner());
to_spinner.setOnItemSelectedListener(new spinner());
quantity.addTextChangedListener(new TextWatcher()
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void afterTextChanged(Editable s)
val = s.toString();
int flag = 0;
if (val.equals(""))
flag = 1;
val = "0";
if (val.equals("-") || val.equals(".")) flag = 1;
if (choice_spinner.equals("Temperature"))
if (flag == 0)
double celcius = Double.parseDouble(val);
if (from_spinner_position == 2)
celcius = celcius - Double.parseDouble("32");
celcius = celcius * Double.parseDouble("0.55555");
else if (from_spinner_position == 1)
celcius = celcius - Double.parseDouble("273.15");
if (to_spinner_position == 2)
celcius = celcius * Double.parseDouble("1.8");
celcius = celcius + Double.parseDouble("32");
else if (to_spinner_position == 1)
celcius = celcius + Double.parseDouble("273.15");
print_exponent(result, celcius);
else
result.setText("");
else
if (flag == 0)
double temp = Double.parseDouble(val);
temp = temp / Double.parseDouble(array[choice_spinner_pos][from_spinner_position].toString());
temp = temp * Double.parseDouble(array[choice_spinner_pos][to_spinner_position].toString());
temp = temp / Double.parseDouble(array[choice_spinner_pos][0].toString());
print_exponent(result, temp);
else
result.setText("");
);
//Method to print the numbers in exponent form
public void print_exponent(TextView view, double temp)
String temp1 = "" + temp;
String temp2 = "";
int bds = 0;
for (char c : temp1.toCharArray())
if (c == 'E')
temp2 += " e ";
bds = 1;
else
if (bds == 1)
if (c == '-')
temp2 += c;
else
temp2 = temp2 + "+" + c;
else
temp2 += c;
bds = 0;
view.setText(temp2);
//This method takes a big decimal number and convert that to the exponent from and scale is mantissa
private static String format(BigDecimal x, int scale)
NumberFormat formatter = new DecimalFormat("0.0E0");
formatter.setRoundingMode(RoundingMode.HALF_UP);
formatter.setMinimumFractionDigits(scale);
return formatter.format(x);
//Spinner class to select spinner and also add gender
class spinner implements AdapterView.OnItemSelectedListener
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (parent.getId() == R.id.conversion_type)
quantity.getText().clear();
result.setText("");
choice_spinner = parent.getItemAtPosition(position).toString();
choice_spinner_pos = position;
from_spinner.setEnabled(true);
to_spinner.setEnabled(true);
if (choice_spinner.equals("Area") == true)
adapter_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_area);
to_spinner.setAdapter(adapter_area);
else if (choice_spinner.equals("Length") == true)
adapter_length.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_length);
to_spinner.setAdapter(adapter_length);
else if (choice_spinner.equals("Temperature") == true)
adapter_temperature.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_temperature);
to_spinner.setAdapter(adapter_temperature);
else if (parent.getId() == R.id.from_spinner)
quantity.getText().clear();
result.setText("");
from_spinner_string = parent.getItemAtPosition(position).toString();
from_spinner_position = position;
@Override
public void onNothingSelected(AdapterView<?> parent)
Toast.makeText(getApplicationContext(), "Please Select a category", Toast.LENGTH_SHORT).show();
from_spinner.setEnabled(false);
to_spinner.setEnabled(false);
public void clear_unit (View view)
quantity.setText("");
result.setText("");
val = "";
【问题讨论】:
【参考方案1】:根本原因
在MainActivity
中,unit_converter()
方法负责Spinner
的初始化,设置适配器,设置监听器为Spinner
,但是你在activity启动的时候忘记调用这个方法了。
解决方案
在活动开始时调用unit_converter()
方法。
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unit_converter();
更新
当用户选择from_spinner
和to_spinner
时,您不会更新from_spinner_position
和to_spinner_position
值。将您的代码更改为:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (parent.getId() == R.id.conversion_type)
quantity.getText().clear();
result.setText("");
choice_spinner = parent.getItemAtPosition(position).toString();
choice_spinner_pos = position;
from_spinner.setEnabled(true);
to_spinner.setEnabled(true);
if (choice_spinner.equals("Area") == true)
adapter_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_area);
to_spinner.setAdapter(adapter_area);
else if (choice_spinner.equals("Length") == true)
adapter_length.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_length);
to_spinner.setAdapter(adapter_length);
else if (choice_spinner.equals("Temperature") == true)
adapter_temperature.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_temperature);
to_spinner.setAdapter(adapter_temperature);
else if (parent.getId() == R.id.from_spinner)
quantity.getText().clear();
result.setText("");
from_spinner_string = parent.getItemAtPosition(position).toString();
from_spinner_position = position;
else if (parent.getId() == R.id.from_spinner)
from_spinner_position = position;
else if (parent.getId() == R.id.to_spinner)
to_spinner_position = position;
【讨论】:
哇!有效!!!但我再次需要你的帮助......它现在可以工作,但如果我输入一个数值,结果仍然是数值......它不会转换它..我输入一个数字将它从摄氏度转换为开尔文但是它仍然打印出相同的数字......不是转换......你能帮忙吗? @Fedan 让我再看看源代码,让你知道问题出在哪里。 好的,我在等……如果您能帮帮我,我将不胜感激? @Fedan 如果您有任何问题,可以在 *** 上发布,很多人会帮助您。顺便说一句,作为初学者,您应该观看一些视频、教程来学习 Android 编程的基础知识(您可以在 Youtube、blogspot、android 开发者网站等上找到它们)。它将帮助您更快、更好地开发 Android 应用程序。 如果这个答案解决了您的问题,那么您可以将其标记为答案或投票。它将帮助我在未来改进我的答案。快乐编码:)以上是关于计算长度、面积和温度的两个spinner的乘法问题的主要内容,如果未能解决你的问题,请参考以下文章