如何禁用从显示出来AutoCompleteTextView的下拉?AutoCompleteTextView

由网友(别说我不靠谱只是你不着调)分享简介:我用下面的code,设置文本的AutoCompleteTextView场。但我注意到,当我设置特定的文本(不是所有文字,但是一些)给它,它会自动弹出的下拉。如果我没有要求的重点,这将是更好,但只有更好,没有完全没事。我试过dissmissDropDwon(),它并不能帮助。那么,有没有什么办法可以阻止显示设置文本和重点...

我用下面的code,设置文本的AutoCompleteTextView场。但我注意到,当我设置特定的文本(不是所有文字,但是一些)给它,它会自动弹出的下拉。如果我没有要求的重点,这将是更好,但只有更好,没有完全没事。我试过dissmissDropDwon(),它并不能帮助。那么,有没有什么办法可以阻止显示设置文本和重点后向上下拉?

I use the following code to set text to an AutoCompleteTextView field. But I noticed that when I set certain text (not all text, but some) to it, it will automatically pop up the drop-down. If I do not request focus, it would be better, but just better, not entirely all right. I tried dissmissDropDwon(), it doesn't help. So, Is there any way to stop the drop-down from showing up after setting text and focus to it?

actv.setText("Tim Hortons");
actv.setSelection(0, actv.getText().length());
actv.requestFocus();
actv.dismissDropDown();    // doesn't help

感谢您!

推荐答案

要回答我的问题,以防有人有同样的问题:

To answer my own question in case someone had the same problem:

AutoCompleteTextView的一个特点是,如果您以编程方式改变它的文本,它会下拉选择列表,如果以下两个条件:1,具有焦点; 2.该列表的长度超过30出头的项目。

One characteristics of AutoCompleteTextView is that if you change its text programmatically, it will drop down the selection list if the following two conditions are met: 1. It has focus; 2. The list is longer than 30-something items.

此行​​为实际上,恕我直言,存在设计缺陷。当程序设置文本的AutoCompleteTextView,那岂不是文本已经是正确的,没有一点弹出过滤列表供用户进一步选择。

This behavior is actually, IMHO, a design flaw. When the program sets text to an AutoCompleteTextView, it would mean that the text is already correct, there is no point to popup the filtered list for user to further choose from.

actv.setText("Tim Hortons"); 
actv.setSelection(0, actv.getText().length()); 
actv.requestFocus(); 
actv.dismissDropDown();    // doesn't help 

在上面的code,不是requestFocus()强制ACTV获取焦点,而这将导致下拉弹出。我试着不去reqeuest焦点,相反,我设置文本​​后调用clearFocus()。但行为很....不自然。 dissmissDropdown()并没有帮助,因为....我不知道,只是没有帮助。因此,有很多蒋张关系后,我想出了这个解决方法:

In the above code, requestFocus() forces the ACTV to get the focus, and this causes the drop-down to pop up. I tried not to reqeuest focus, instead, I called clearFocus() after setting text. But the behavior is very .... unnatural. dissmissDropdown() doesn't help because .... I don't know, it just doesn't help. So, after much strugle, I came up with this work-around:

当初始化窗口小部件,我记得在一类领域的适配器。

更​​改上述code到: When initializing the widget, I remembered the adapter in a class field.

Change the above code to:

mAdapter = (ArrayAdapter<String>)actv.getAdapter(); // mAdapter is a class field        
actv.setText("Tim Hortons"); 
actv.setSelection(0, actv.getText().length()); 
actv.setAdapter((ArrayAdapter<String>)null); // turn off the adapter
actv.requestFocus();
Handler handler = new Handler() {
public void handleMessage(Message msg) {
    ((AutoCompleteTextView)msg.obj).setAdapter(mAdapter);
    };
Message msg = mHandler.obtainMessage();
msg.obj = actv;
handler.sendMessageDelayed(msg, 200);   // turn it back on after 200ms

这里的窍门是设置ACTV的适配器为null。当然,因为没有适配器,系统将不会弹出下拉。但是,该消息将重置适配器回ACTV 200ms的编程的延迟后,和ACTV通常会照常上班。

Here the trick is set the ACTV's adapter to null. Because there is no adapter, of course the system will not pop up the drop-down. But the message will reset the adapter back to the ACTV after the programmed delay of 200ms, and the ACTV will work normally as usual.

这对我的作品!

阅读全文

相关推荐

最新文章