我遇到的问题是应用程序无法执行我设计的任务。我成功地运行了这个应用程序,但是当我单击任何一个微调器时,列表根本就没有下降。所有的纺纱机都没有React,我也不知道问题出在哪里。我想尽一切办法来解决这个问题,但我做不到。我运行代码很多次都没有收到错误消息,但是微调器仍然不工作。有人能帮忙吗?这个应用程序可以转换长度,面积和温度。
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>
活动\u 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_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:id="@+id/first_linear"
android:layout_marginRight="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
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_width="fill_parent"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_height="1dp"
android:id="@+id/text_some"
android:background="#cbcbcb"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_below="@id/first_linear"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:id="@+id/second_linear"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:hint="edit_text_from"
android:id="@+id/from_edit"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:inputType="numberSigned|numberDecimal"
android:layout_marginLeft="30dp" />
<Button
android:layout_width="0dp"
android:layout_height="45dp"
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_width="match_parent"
android:layout_height="1dp"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="1dp"
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条答案
按热度按时间lvjbypge1#
根本原因
在
MainActivity
,的unit_converter()
方法负责初始化Spinner
,设置适配器,为Spinner
,但您忘记在活动启动时调用此方法。解决方案
打电话给
unit_converter()
方法。更新
当用户选择
from_spinner
以及to_spinner
,不更新from_spinner_position
以及to_spinner_position
价值观。将代码更改为: