-1

我需要你的帮助,这真的让我发疯,我真的找不到解决方案所以我想要创建的是一个适用于 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();
}


}
4

1 回答 1

1

主要活动:

public class MainActivity extends Activity {

private Spinner spinner1, spinner2;
private Button btnSubmit;
private EditText from;
private TextView to;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    from = (EditText) findViewById(R.id.InputEditText);
    to = (TextView) findViewById(R.id.OutputTextView);

    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 onClick(View v) {

    int index1 = spinner1.getSelectedItemPosition();
    int index2 = spinner2.getSelectedItemPosition();
    float value = Float.parseFloat(from.getText().toString());

    /* you have 8 units to convert from and to.
     * that means 8*8 = 64 cases!
     * to minimize work we convert from any selected unit to Centimeter
     * then we convert from Centimeter to the desired unit.
     */

    // ratios from google convertor
    float ratio[] = {0.393701f, 0.0328084f, 0.0109361f, 6.21371e-6f, 10.0f, 1.0f, 0.01f, 1.0e-5f};
    float result = value / ratio[index1] * ratio[index2];
    to.setText(result+"");
}

}

布局:

<LinearLayout 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"
    tools:context=".MainActivity" 
    android:orientation="vertical">

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<EditText
    android:id="@+id/InputEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/OutputTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

<Button
    android:id="@+id/btnSubmit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="onClick" />

</LinearLayout>

祝你好运

于 2013-09-23T13:25:31.820 回答