微调器的onItemSelected方法没有被调用。 (更新为标题)

时间:2019-01-13 16:39:55

标签: java android

我正在尝试根据Spinner中的选定项来更改TextView文本。我该怎么办?

我在StackOverflow上看了一些YouTube视频和大约50页。我尝试了许多不同的操作,例如从微调器切换位置以更改TextView,查看是否从微调器中选择了特定的字符串,然后使用该字符串来更改TextView,但似乎无济于事。 如果用户在微调器上选择了“英寸为厘米”,则需要conversionFrom TextView使其显示为“英寸”,并且需要conversionTo TextView来表示“厘米”,依此类推。

  

编辑:

     

我尝试制作一个微调器类并使用它,但仍然没有调用   onItemSelect方法。

  public class MeasurementConverter extends Activity implements TextView.OnEditorActionListener, AdapterView.OnItemSelectedListener {

    // defining variables for widgets
    Spinner spinner;
    private EditText measurementInput;
    private TextView conversionFrom;
    private TextView conversionTo;
    private TextView convertedNumber;

    private double milesToKilo = 1.6093;
    private double kiloToMiles = 0.6214;
    private double inchToCent = 2.54;
    private double centToInch = 0.3937;


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

        spinner = (Spinner) findViewById(R.id.spinner1);
        measurementInput = (EditText) findViewById(R.id.measurementInput);
        conversionFrom = (TextView) findViewById(R.id.conversionFrom);
        conversionTo = (TextView) findViewById(R.id.conversionTo);
        convertedNumber = (TextView) findViewById(R.id.convertedNumber);

        ArrayAdapter<CharSequence> adapter = 
        ArrayAdapter.createFromResource(this, R.array.typeArray, 
        android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(
        android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onResume()
    {
        super.onResume();
        calculateAndDisplay();
    }

    public void calculateAndDisplay()
    {
        // get the input
        String input = measurementInput.getText().toString();
        double inputAmount;

        if(input.equals("")) {
            inputAmount = 0;
        }else {
            inputAmount = Float.parseFloat(input);
        }

        // miles to kilometers todo
        // kilometers to miles todo
        // inches to centimeters todo
        // centimeters to inches todo
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if(actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED){
            calculateAndDisplay();
        }
        return false;
     }

     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int 
     position, long id) 
      {
        switch(position)
        {
            case 0:
                conversionFrom.setText("Miles");
                conversionTo.setText("Kilometers");
                break;
            case 1:
                conversionFrom.setText("Kilometers");
                conversionTo.setText("Miles");
                break;
            case 2:
                conversionFrom.setText("Inches");
                conversionTo.setText("Centimeters");
                break;
            case 3:
                conversionFrom.setText("Centimeters");
                conversionTo.setText("Inches");
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
  } 
<resources>
<string name="app_name">MeasurementConverter</string>
    <string-array name="typeArray">
        <item>Miles to Kilometers</item>
        <item>Kilometers to Miles</item>
        <item>Inches to Centimeters</item>
        <item>Centimeters to Inches</item>
    </string-array>
    <string name ="conversion_From"></string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        tools:context=".MainActivity">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/conversionTextView"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="Conversion"
                    android:textSize="26dp"
                    android:textStyle="bold" />

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

        </LinearLayout>
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/conversionFrom"
                    android:layout_width="135dp"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:padding="10dp"
                    android:textStyle="bold"
                    android:text=""
                    android:textSize="20sp"/>

            <EditText
                    android:id="@+id/measurementInput"
                    android:layout_width="228dp"
                    android:layout_height="match_parent"
                    android:ems="8"
                    android:inputType="numberDecimal"
                    android:text=""
                    android:textSize="22sp"/>
        </LinearLayout>
        <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="wrap_content">
            <TextView
                    android:id="@+id/conversionTo"
                    android:layout_width="136dp"
                    android:layout_height="50dp"
                    android:textAlignment="center"
                    android:padding="8dp"
                    android:textStyle="bold"
                    android:text="@+id/conversionTo"
                    android:textSize="20sp"/>

            <TextView
                    android:id="@+id/convertedNumber"
                    android:layout_width="228dp"
                    android:layout_height="match_parent"
                    android:padding="8dp"
                    android:text="10.52"
                    android:textSize="20sp"/>
        </LinearLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout> 

conversionFrom文本视图始终为空白。 converstionTo文本视图始终为false。 也许我缺少一段代码使其可见?

在此之前(我已修复),我的问题是Spinner上根本没有显示文本。为了解决这个问题,我终于找到了解决方案。这个人的Spinner XML文件中有android:entries="@array/typeArray",这使我的文本显示在Spinner中。因此,也许我在这里缺少更改TextView文本的内容。

0 个答案:

没有答案