如何交换两个AutoCompleteTextView的值

时间:2017-07-31 10:37:39

标签: android android-spinner autocompletetextview

我添加了两个autoCompleteTextView和简单的微调器,现在我想通过单击Imageview来交换它们的值。但每当我点击ImageView时,我的应用就会重启。没有错误或警告。
这是Java代码(更新): -

public class location extends AppCompatActivity {
    private AutoCompleteTextView autoText1,autoText2,autoText3;
    private int selected,selected1;
   String[] locnames,locnames1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bus_route);

    locnames = getResources().getStringArray(R.array.Loc_names);
    locnames1 = getResources().getStringArray(R.array.Loc_names);

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item,
            locnames);

    autoText1 =(AutoCompleteTextView) findViewById(R.id.actv1);
    autoText1.setAdapter(arrayAdapter);
    autoText1.setThreshold(1);
    autoText1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //autoText1.showDropDown();
            hideKeyBoard(view);//move here
            //String selection = (String) parent.getItemAtPosition(position);
            selected = position;
             }
    });
    autoText1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {
            autoText3.setText(null);
            autoText1.showDropDown(); }
    });
    final ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item,
            locnames1);


    autoText2 =(AutoCompleteTextView) findViewById(R.id.actv2);
    autoText2.setAdapter(arrayAdapter1);
    autoText2.setThreshold(1);
    autoText2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //autoText2.showDropDown();
            hideKeyBoard(view);//move here
           // String selection = (String) parent.getItemAtPosition(position);
             selected1 = position;
            }
    });
    autoText2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg1) {
            autoText3.setText(null);
            autoText2.showDropDown(); }
    });


         final ImageView arrow = (ImageView) findViewById(R.id.arrow1);    
        arrow.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View arg3)
            {

                autoText1.setText(locnames[selected1]);
            autoText1.clearFocus();
            autoText2.setText(locnames1[selected]);

            autoText2.clearFocus();
            int temp = selected1;
           selected1 = selected;
           selected = temp;


            //arrow.setRotation(arrow.getRotation() + 180);

            mCurrRotation %= 360;
            float fromRotation = mCurrRotation;
            float toRotation = mCurrRotation += 180;

            final RotateAnimation rotateAnim = new RotateAnimation(
                    fromRotation, toRotation, arrow.getWidth()/2, arrow.getHeight()/2);

            rotateAnim.setDuration(500);
            rotateAnim.setFillAfter(true);

            arrow.startAnimation(rotateAnim);

            }

        });
    }

同时检查旋转输出,它也没有错误,但仍然没有发生任何事情。感谢您的帮助。

编辑:
我已经想出了轮换。它可以通过

来完成
arrow.setRotation(arrow.getRotation() + 180);

或者如果想要设置动画,则首先声明变量
private int mCurrRotation = 0;
然后再

            mCurrRotation %= 360;
            float fromRotation = mCurrRotation;
            float toRotation = mCurrRotation += 180;

            final RotateAnimation rotateAnim = new RotateAnimation(
                    fromRotation, toRotation, arrow.getWidth()/2, arrow.getHeight()/2);

            rotateAnim.setDuration(1000);
            rotateAnim.setFillAfter(true);

            arrow.startAnimation(rotateAnim);

1 个答案:

答案 0 :(得分:0)

由于没有可用于在AutoCompleteTextView上设置选择的直接方法,您需要访问该数组并在AutoCompleteTextView上设置文本。
这是解决方案。

public class StackOverflowActivity extends AppCompatActivity {
AutoCompleteTextView actvFrom,actvTo;
ImageView ivSwap;
private static final String[] COUNTRIES = new String[] {
        "Belgium", "France", "Italy", "Germany", "Spain"
};

int fromIndex,toIndex;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stackoverflow);
    actvFrom = (AutoCompleteTextView) findViewById(R.id.actvFrom);
    actvTo = (AutoCompleteTextView) findViewById(R.id.actvTo);
    ivSwap = (ImageView) findViewById(R.id.ivSwap);

    final ArrayAdapter<String> fromAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,COUNTRIES);
    ArrayAdapter<String> toAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,COUNTRIES);

    actvFrom.setAdapter(fromAdapter);
    actvFrom.setThreshold(1);
    actvTo.setAdapter(toAdapter);
    actvTo.setThreshold(1);


    actvFrom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            fromIndex = position;
        }
    });
    actvFrom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actvFrom.showDropDown();
        }
    });
    actvTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            toIndex = position;
        }
    });
    actvTo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actvTo.showDropDown();
        }
    });

    ivSwap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actvFrom.setText(COUNTRIES[toIndex]);

            actvTo.setText(COUNTRIES[fromIndex]);

            int temp = toIndex;
            toIndex = fromIndex;
            fromIndex = temp;
            actvTo.clearFocus();
            actvFrom.clearFocus();
        }
    });

}
}