更改微调器项目时无法更改值

时间:2016-02-02 13:00:49

标签: java android spinner

我正在做的是制作一个单位转换器应用程序虽然它运行良好,但是当我将微调项目从 INCHES更改为FOOT 时,值没有改变,但是当我在EditText框中添加或删除一个数字,并将其更新为脚参数值。

我想要的是当我在文本框中输入值时,它会根据Spinner中选择的项目更新值,例如:如果我写23并且Spinner项目是 < em> INCHES 然后根据英寸转换显示所有值,当我点击Spinner并将其更改为 FEET 时将根据英尺转换向我显示值但是在我的情况下这不会发生这种情况只发生在我在文本框中添加/删除某些数字时它会更新为FEET。

这是Length.java文件

package com.example.farrukh.convertmee;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Length extends Fragment implements AdapterView.OnItemSelectedListener

{
    String[] Lunits = {"Inches", "Feet", "Yards", "Miles", "Km", "Meters" ,"Centimeters" };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       // return inflater.inflate(R.layout.length_layout,null);


        final View myFragmentView = inflater.inflate(R.layout.length_layout, container, false); //get laength.xml
        final Spinner spin = (Spinner)myFragmentView.findViewById(R.id.spinner);
        final  TextView a=(TextView)myFragmentView.findViewById(R.id.textView9);
        final  TextView b=(TextView)myFragmentView.findViewById(R.id.textView10);
        final  TextView c=(TextView)myFragmentView.findViewById(R.id.textView11);
        final  TextView d=(TextView)myFragmentView.findViewById(R.id.textView12);
        final  TextView e=(TextView)myFragmentView.findViewById(R.id.textView13);
        final  TextView f=(TextView)myFragmentView.findViewById(R.id.textView14);
        final  TextView g=(TextView)myFragmentView.findViewById(R.id.textView15);

        final  EditText text =(EditText)myFragmentView.findViewById(R.id.editText);

        spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {

                text.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {


                    }

                    @Override
                    public void afterTextChanged(Editable s)
                    {

                        double ans=0;
                        if( !s.toString().equals("") && s.toString().length() > 0 )
                        {

                            ans = Double.parseDouble(s.toString());
                        }

                        if(spin.getSelectedItem().equals("Inches"))
                        {
                            double inches = ( ans * 1 );
                            a.setText(" "+inches);

                            double feet = ( ans * 0.0833 );
                            b.setText(" "+feet);

                            double yards = ( ans * 0.02777778 );
                            c.setText(" " + yards);

                            double miles = ( ans * (1/63360) );
                            d.setText(" " + miles);

                            double kilometers = ( (ans/39370) );
                            e.setText(" " + kilometers);

                            double meters = ( ans * 0.0254 );
                            f.setText(" " + meters);

                            double centimeters = ( ans * 2.54 );
                            g.setText(" "+centimeters);
                        }

                        if (spin.getSelectedItem().equals("Feet"))
                        {
                            double inches = ( ans * 12 );
                            a.setText(" "+inches);

                            double feet = ( ans * 1 );
                            b.setText(" "+feet);

                            double yards = ( ans * 0.3333333 );
                            c.setText(" " + yards);

                            double miles = ( ans * (0.0001893939) );
                            d.setText(" " + miles);

                            double kilometers = ( ans * 0.0003048 );
                            e.setText(" " + kilometers);

                            double meters = ( ans * 0.3048 );
                            f.setText(" " + meters);

                            double centimeters = ( ans * 30.48 );
                            g.setText(" "+centimeters);
                        }

                    }


                });


            }


            public void onNothingSelected(AdapterView<?> parent) {

            }
        });




        ArrayAdapter aa = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,Lunits);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);


        return myFragmentView;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

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

    }
}

2 个答案:

答案 0 :(得分:1)

删除text.addTextChangedListener(new TextWatcher() {侦听器。每当您从微调器中选择不同的项目时,系统会调用onItemSelected,但您内部会有TextWatcher,只有当EditText中的文字发生更改时才会调用该文件。

如果您想同时进行转换(微调项目选择和文本更改),请在单独的方法中移动整个转换代码,并从两个侦听器(OnItemSelectedListenerTextWatcher调用它但是 TextWatcher需要独立,而不是在另一个侦听器内。

答案 1 :(得分:1)

将您的EditText ans TextView声明为类变量(不需要 final ),并在TextWatcher之外添加Sipnner听众。

这是我的建议:

 public class Length extends Fragment implements AdapterView.OnItemSelectedListener

    {
        String[] Lunits = {"Inches", "Feet", "Yards", "Miles", "Km", "Meters" ,"Centimeters" };

        EditText mEditText;
        TextView a, b, c, d, e, f, g;
        String mSelectedUnit = "";

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           // return inflater.inflate(R.layout.length_layout,null);


            final View myFragmentView = inflater.inflate(R.layout.length_layout, container, false); //get laength.xml
            Spinner spin = (Spinner)myFragmentView.findViewById(R.id.spinner);
            a=(TextView)myFragmentView.findViewById(R.id.textView9);
            b=(TextView)myFragmentView.findViewById(R.id.textView10);
            c=(TextView)myFragmentView.findViewById(R.id.textView11);
            d=(TextView)myFragmentView.findViewById(R.id.textView12);
            e=(TextView)myFragmentView.findViewById(R.id.textView13);
            f=(TextView)myFragmentView.findViewById(R.id.textView14);
            g=(TextView)myFragmentView.findViewById(R.id.textView15);

            mEditText =(EditText)myFragmentView.findViewById(R.id.editText);


            ArrayAdapter aa = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,Lunits);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spin.setAdapter(aa);
            spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                     mSelectedUnit = Lunits[position];
                     convertValue();

                }

                public void onNothingSelected(AdapterView<?> parent) {

                }
            });


           mEditText.addTextChangedListener(new TextWatcher() {

              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
              }

              @Override
              public void afterTextChanged(Editable s)
              {
                 convertValue();
              }

          });

            return myFragmentView;
        }

        private void convertValue(){
           double ans=0;
           String s = mEditText.getText().toString(); 

           if( !s.toString().equals("") && s.length() > 0 )
           {
              ans = Double.parseDouble(s);
           }

           if(mSelectedUnit.equals("Inches"))
           {
              double inches = ( ans * 1 );
              a.setText(" "+inches);

              double feet = ( ans * 0.0833 );
              b.setText(" "+feet);

              double yards = ( ans * 0.02777778 );
              c.setText(" " + yards);

              double miles = ( ans * (1/63360) );
              d.setText(" " + miles);

              double kilometers = ( (ans/39370) );
              e.setText(" " + kilometers);

              double meters = ( ans * 0.0254 );
              f.setText(" " + meters);

              double centimeters = ( ans * 2.54 );
              g.setText(" "+centimeters);
           }

           if (mSelectedUnit.equals("Feet"))
           {
               double inches = ( ans * 12 );
               a.setText(" "+inches);

               double feet = ( ans * 1 );
               b.setText(" "+feet);

               double yards = ( ans * 0.3333333 );
               c.setText(" " + yards);

               double miles = ( ans * (0.0001893939) );
               d.setText(" " + miles);

               double kilometers = ( ans * 0.0003048 );
               e.setText(" " + kilometers);

               double meters = ( ans * 0.3048 );
               f.setText(" " + meters);

               double centimeters = ( ans * 30.48 );
               g.setText(" "+centimeters);
           }

         }
      }
    }