无法实现微调器

时间:2014-11-05 19:49:17

标签: java android eclipse spinner

我是Java和Android开发的新手。我在Android应用程序中实现微调器时遇到问题。微调器填充得很好,但是当转子中的索引发生变化时,我无法将它转换为转换变量的标签。从本质上讲,我无法上班的类是selectOption()或public void onItemSelected(AdapterView parent,View view,int position,long id)class。

非常感谢任何帮助。

package com.example.conversion;
import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;


public class MainActivity extends ActionBarActivity implements OnEditorActionListener, AdapterView.OnItemSelectedListener{

//Graphical Variables
private Spinner option;
private TextView inputTextView;
private TextView outputTextView;
private EditText conversionInput;
private TextView conversionOutput;

//Conversions
private float 
        milesToKil = 1.6093f,
        kilToMiles = 0.6214f,
        inchToCent = 2.54f,
        centToInch = 0.3937f;

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

    //get IDs
    option = (Spinner) findViewById(R.id.option);
    inputTextView = (TextView) findViewById(R.id.inputTextView);
    outputTextView = (TextView) findViewById(R.id.outputTextView);
    conversionInput = (EditText) findViewById(R.id.conversionInput);
    conversionOutput = (TextView) findViewById(R.id.conversionOutput);
    conversionInput.setOnEditorActionListener(this);

    //Add values to spinner
    addToSpinner();
    //selectOption();

    display("");
}

//Spinner
public void addToSpinner() {

    option = (Spinner) findViewById(R.id.option);
    List<String> list1 = new ArrayList<String>();
    list1.add("Miles To Kilometers");
    list1.add("Kilometers To Miles");
    list1.add("Inches To Centimeters");
    list1.add("Centimeters to Inches");
    ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list1);
    dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    option.setAdapter(dataAdapter1); 
}

/*public void selectOption()
{
    int position;

    position = option.getSelectedItemPosition();
    String cAmountString = conversionInput.getText().toString();
    float temp = Float.parseFloat(cAmountString);

    if(position == 0)
    {
        inputTextView.setText("Miles");
        outputTextView.setText("Kilometers");
        convertMilesToKilometers(temp);
    }
    else if(position == 1)
    {
        inputTextView.setText("Kilometers");
        outputTextView.setText("Miles");
        convertKilometersToMiles(temp);
    }
    else if(position == 2)
    {
        inputTextView.setText("Inches");
        outputTextView.setText("Centimeters");
        //convertInchesToCentimeters(cAmountString);
    }
    else if(position == 3)
    {
        inputTextView.setText("Centimeters");
        outputTextView.setText("Inches");
        //convertCentimetersToInches(cAmountString);
    }

}*/



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}



public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
    display("");
    return false;
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    TextView myText = (TextView) view;
    Toast.makeText(this, "You selected " + myText.getText() + " ", Toast.LENGTH_LONG).show();

    String cAmountString = conversionInput.getText().toString();
    float temp = Float.parseFloat(cAmountString);

    if(position == 0)
    {
        inputTextView.setText("Miles");
        outputTextView.setText("Kilometers");
        convertMilesToKilometers(temp);
    }
    else if(position == 1)
    {
        inputTextView.setText("Kilometers");
        outputTextView.setText("Miles");
        convertKilometersToMiles(temp);
    }
    else if(position == 2)
    {
        inputTextView.setText("Inches");
        outputTextView.setText("Centimeters");
        //convertInchesToCentimeters(cAmountString);
    }
    else if(position == 3)
    {
        inputTextView.setText("Centimeters");
        outputTextView.setText("Inches");
        //convertCentimetersToInches(cAmountString);
    }

}

 public void display(String displayText)
    {       
        if(displayText.equals(""))
        {
            conversionOutput.setText("0.0");
        }
        else
        {
            conversionOutput.setText(displayText);
        }
    }

private void convertMilesToKilometers(float cAmount) {

    float total;
    total = cAmount * milesToKil;
    String temp2 = total + "";
    display(temp2);     
}

private void convertKilometersToMiles(float cAmount) {

    float total;
    total = cAmount * kilToMiles;
    String temp2 = total + "";
    display(temp2);     
}

private void convertInchesToCentimeters(String cAmount) {

    float temp;
    float total;
    temp = Float.parseFloat(cAmount);
    total = temp * inchToCent;
    String temp2 = total + "";
    display(temp2);
}

private void convertCentimetersToInches(String cAmount) {

    float temp;
    float total;
    temp = Float.parseFloat(cAmount);
    total = temp * centToInch;
    String temp2 = total + "";
    display(temp2);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}
}

2 个答案:

答案 0 :(得分:2)

在你的oncreate方法中,在行addToSpinner();

之后

添加此行代码

option.setOnItemSelectedListener(this);

它会起作用。

答案 1 :(得分:0)

您需要为您的微调器设置onItemSelectedListener。 覆盖侦听器中的onItemSelected方法。

例如

option.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // if you have the spinner
            String toastText = option.getSelectedItem().toString();
            // if you havent
            String toastText = parent.getItemAtPosition(pos).toString();

        });

希望这可以带给你更多。