0

我有一个 AutoCompleteTextView,当您在 TextView 中键入内容时,它会给出一个建议列表。但是,当您选择建议时,您仍然必须按 Enter 键,不仅要从屏幕上移除键盘,还要提交您选择的建议。我正在尝试删除按 Enter 的这个额外步骤,因为用户没有在我的应用程序中建立连接,他们在 TextView 中选择建议后必须按 Enter 键。有什么建议/帮助吗?

这是我得到的建议,但似乎没有用。

element_image = (ImageView) findViewById(R.id.element_bullet);
    //Get the string array of the radionuclide names that will auto complete.
    radionuclideNames = getResources().getStringArray(R.array.radionuclide_names);
    //Get a reference to the AutoCompleteTextView in the layout.
    radionuclideTextView = (AutoCompleteTextView) findViewById(R.id.radionuclide_autocomplete);
    //Create the adapter and set it to the AutoCompleteTextView.
    ArrayAdapter<String> adapter = 
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, radionuclideNames);
    //Apply the adapter to the AutoCompleteTextView.
    radionuclideTextView.setAdapter(adapter);
    //Listen for when enter is pressed.
    radionuclideTextView.setOnItemSelectedListener(new OnItemSelectedListener() { 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
            switch(parent.getId())
            {
            case R.id.radionuclide_autocomplete:
                element_image.setImageResource(R.drawable.selectedbulletstate);
                break;
            }
        } 
        public void onNothingSelected(AdapterView<?> parent) { 
          //TODO:
        }
    }); 
4

2 回答 2

1

尝试这样的事情:

auto.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(auto.getWindowToken(), 0);
        }
    });

...但提交您选择的建议

当您选择其中一个选项时,下拉菜单应自动关闭。

于 2013-03-26T18:02:19.763 回答
0

当然你有这样的东西,使用

autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener() { 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
          //TODO:
        } 
        public void onNothingSelected(AdapterView<?> parent) { 
          //TODO:
        }
    }); 
于 2013-03-26T16:29:32.737 回答