我需要你的帮助,这真的让我发疯,我真的找不到解决方案所以我想要创建的是一个适用于 android 的转换器应用程序,这是我的代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Input Number Here"
android:inputType="number" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Converts to:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="103dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert" />
<EditText
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
MainActivity.java
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list1 = new ArrayList<String>();
list1.add("Inches");
list1.add("Feet");
list1.add("Yard");
list1.add("Miles");
list1.add("Milimeter");
list1.add("Centimeter");
list1.add("Meter");
list1.add("Kilometer");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list2 = new ArrayList<String>();
list2.add("Inches");
list2.add("Feet");
list2.add("Yard");
list2.add("Miles");
list2.add("Milimeter");
list2.add("Centimeter");
list2.add("Meter");
list2.add("Kilometer");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
if(selected1.equals("Inches"))
{
if (selected2.equals("Feet"))
{
//do calculation
}
else if (selected2.equals("unit"))
{
//do calculation
}
else if (selected2.equals("otherunit"))
{
//do calculation
}
else
{
//ERROR
}
}
if (selected1.equals("m"))
{
if (selected2.equals("Yard"))
{
//do calculation
}
else if (selected2.equals("unit"))
{
//do calculation
}
else if (selected2.equals("otherunit²"))
{
//do calculation
}
else
{
//ERROR
}
}
//repeat for all the possible units
}
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
}