[英]Can't get user entered text from android AutoCompleteTextView after item selected
I have an AutoCompleteTextView in a Fragment (android.support.v4.app). 我在片段(android.support.v4.app)中有一个AutoCompleteTextView。 After the user selects an item in the dropdown, I want to retain the text they entered.
用户在下拉菜单中选择一个项目后,我想保留他们输入的文本。 Here's what I have.
这就是我所拥有的。
First the class definition: 首先是类的定义:
public class MyFragment extends Fragment
implements
AutoCompleteTextView.OnDismissListener,
AutoCompleteTextView.OnKeyListener,
AutoCompleteTextView.OnEditorActionListener {
In onViewCreated() I have these assignments: 在onViewCreated()中,我有以下分配:
myAutoComplete.setOnDismissListener(MyFragment.this);
myAutoComplete.setOnKeyListener( MyFragment.this );
myAutoComplete.setOnEditorActionListener(MyFragment.this);
And I've added these methods: 并且我添加了以下方法:
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
Log.i("************(arg0) ", arg0.toString());
Log.i("************(arg1) ", arg0.toString());
Log.i("***********(keyevent) ", arg2.toString());
return false;
}
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
Log.i("************(arg0) ", arg0.toString());
Log.i("************(arg1) ", arg0.toString());
Log.i("***********(keyevent) ", arg2.toString());
return false;
}
@Override
public void onDismiss() {
// Fires when the dropdownlist is dismissed.
// The event is assigned in onViewCreated
}
My first hope was that the onDismiss() event would fire before the user-entered text was replaced by the selected item. 我的第一个希望是onDismiss()事件将在用户输入的文本替换为所选项目之前触发。 The event, as shown, does fire correctly, but after the users text was replaced with the selected item.
如图所示,该事件确实正确触发,但是在用户文本被选定的项目替换之后。
I then wired up two different key listeners, hoping to capture and store the value while it's being entered. 然后,我连接了两个不同的键侦听器,希望在输入值时捕获并存储该值。 These do not fire.
这些不射击。 After researching this issue, I've learned that key listeners were only designed to work on physical keyboards.
在研究了此问题之后,我了解到按键监听器仅设计用于物理键盘。 Though some people appear to have gotten it to work with soft kybds, broad compatibility appears to be a problem.
尽管有些人似乎已经使它可以与柔软的kybds一起使用,但广泛的兼容性似乎是一个问题。
So I'm stuck, and I don't have any hair left. 所以我被卡住了,没有头发了。 Anyone have any suggestions?
有人有什么建议吗?
@Rushi Ayyappa is right use TextWatcher @Rushi Ayyappa正确使用TextWatcher
autoCompleteTextView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(){
if(isSpinnerAlreadySelected){
autoCompleteTextView1.setEnabled(true);
autoCompleteTextView1.performClick();
}
});
autoCompleteTextView1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(isSpinnerAlreadySelected){
autoCompleteTextView1.setEnabled(false);
//Notify the user he cannot type and why?? Toast??
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.