我已经在我的应用程序中添加了AutoCompleteTextView。在其中我对这两种方法都进行了编码,即afterTextChange()如果一个人写了完整的单词并且不单击proposal和OnItemClickListener()(如果用户单击建议),但是问题在于这两种方法都可以一起工作。如果我使用onItemClick单击建议,则afterTextChange()也可以工作,并且我的代码运行两次。如何防止两种方法不能同时运行。
OnAfterTextChange()代码
actvShop_name.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
actvShop_name.setVisibility(View.VISIBLE);
txtShop.setVisibility(View.GONE);
//do nothing
if ((actvShop_name.getText().length() != 0) && actvShop_name.getText().length() >= 3) {
modelShopDetailsArrayList = databaseHelper.getShopDetail(actvShop_name.getText().toString(), language);
Log.e("TAG", "onKeyDown:Add " + modelShopDetailsArrayList.size() + " " + actvShop_name.getText().toString() + language);
if (modelShopDetailsArrayList.size() != 0) {
HashMap<String, String> shopDetails = session.getSelectedShopDetail();
Log.e("TAG", "onItemClick: " + shopDetails.get("shop_name_nl") + " ");
if (shop_name != null) {
if (shop_name == actvShop_name.getText().toString()) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(actvShop_name.getText().toString());
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setMessage(R.string.delete_cart);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
databaseHelper.deleteCart();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(actvShop_name.getText().toString());
}
});
alertDialog.setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(actvShop_name.getText().toString());
}
OnItemClickListener()代码
actvShop_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// actvShop_name.removeTextChangedListener(textWatcher);
shop_name_entered = adapterView.getItemAtPosition(i).toString();
actvShop_name.setVisibility(View.GONE);
txtShop.setVisibility(View.VISIBLE);
txtShop.setText(shop_name_entered);
HashMap<String, String> shopDetails = session.getSelectedShopDetail();
Log.e("TAG", "onItemClick: " + shopDetails.get("shop_name_nl") + " " + shop_name_entered);
if (shop_name != null) {
if (shop_name.equals(shop_name_entered)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(shop_name_entered);
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setMessage(R.string.delete_cart);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
databaseHelper.deleteCart();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(shop_name_entered);
}
});
alertDialog.setNegativeButton(R.string.no
, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);
fillShopDetails(shop_name_entered);
}
}
});
actvShop_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
actvShop_name.setVisibility(View.VISIBLE);
txtShop.setVisibility(View.GONE);
}
});
txtShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
actvShop_name.setVisibility(View.VISIBLE);
txtShop.setVisibility(View.GONE);
}
});
两个方法同时运行时,对话框打开了两次。
答案 0 :(得分:0)
我也遇到了您现在面临的相同问题。
当我们使用addTextChangedListener
时,如果EditText
发生任何更改,无论是通过键入还是以编程方式设置文本(例如从列表中选择一个值然后设置为Editext)
您可以使用两种方法。
addTextChangedListener
侦听器。为此,请点击here AutoCompleteTextView
上创建一个新视图(可以创建 TextView ),并将视图的可见性默认设置为 GONE 。将像在addTextChangedListener
使用下面的代码中一样,以编程方式播放视图可见性代码:
@Override
public void afterTextChanged(Editable s) {
//do nothing
YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
YOUR_TEXTVIEW.setVisibility(View.GONE);
}
再次在setOnItemClickListener
内,我们需要按照以下方式播放视图可见性,并在下面的新Textview
检查中设置设置的文本:-
actvShop_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.GONE);
YOUR_TEXTVIEW.setVisibility(View.VISIBLE);
}
}
并点击YOUR_AUTO_COMPLTE_TEXTVIEW和YOUR_TEXTVIEW来设置视图可见性,如下所示
YOUR_AUTO_COMPLTE_TEXTVIEW.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
YOUR_TEXTVIEW.setVisibility(View.GONE);
}
});
YOUR_TEXTVIEW.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
YOUR_TEXTVIEW.setVisibility(View.GONE);
}
});