gpt4 book ai didi

android - 自动完成 TextView : AfterTextChange and OnItemClick working together

转载 作者:行者123 更新时间:2023-11-30 05:09:47 27 4
gpt4 key购买 nike

我在我的应用程序中添加了 AutoCompleteTextView。我在其中对这两种方法进行了编码,即 afterTextChange() 如果一个人写了完整的单词并且不点击建议和 OnItemClickListener() 如果用户点击 sugesstion 但问题是这两种方法一起工作。如果我使用 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);
}
});

对话框打开了两次,因为这两种方法同时运行。

最佳答案

我遇到了你现在面临的同样问题。

当我们使用 addTextChangedListener 时,当您的 EditText 发生任何更改时,无论是通过键入还是以编程方式设置文本(例如从中选择一个值),它都会触发列表,然后在 Edittext 上设置)

您可以使用这两种方法。

  1. 从 AutoCompleteTextView 中禁用 addTextChangedListener 监听器。为此点击here
  2. 尝试查看可见性(我已成功使用)。为此,您需要在 AutoCompleteTextView 上创建一个新 View (您可以创建 TextView)并将 View 的可见性设置为 GONE默认情况下,我们将以编程方式播放 View 可见性,就像在 addTextChangedListener 中使用下面的代码

代码:

  @Override
public void afterTextChanged(Editable s) {
//do nothing
YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
YOUR_TEXTVIEW.setVisibility(View.GONE);

}

setOnItemClickListener 中,我们需要再次播放 View 可见性,如下所示,并在新的 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 来设置 View 可见性,如下所示

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);
}
});

关于android - 自动完成 TextView : AfterTextChange and OnItemClick working together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com