[英]How to swap values of two 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);
由於沒有直接方法可用於在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();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.