【问题标题】:AutoCompleteTextView: Remove soft keyboard on back press instead of suggestionsAutoCompleteTextView:在后按时删除软键盘而不是建议
【发布时间】:2015-01-21 02:02:32
【问题描述】:

使用AutoCompleteTextView 时,会出现下拉建议列表,而软键盘仍然可见。这是有道理的,因为键入随后的字符来缩小列表的效率通常要高得多。

但是如果用户想要浏览建议列表,在软件键盘仍然打开的情况下会变得非常乏味(当设备处于横向时,这个问题就更大了)。在没有键盘占用屏幕空间的情况下浏览列表要容易得多。不幸的是,当您按下后退键时,默认行为会首先删除列表(即使在软件版本的后退键中,它会显示“按下此键将隐藏键盘”的图像)。

这是一个简单的例子,展示了我在说什么:

public class Main2 extends Activity {
    private static final String[] items = {
            "One",
            "Two",
            "Three",
            "Four",
            "Five"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AutoCompleteTextView actv = new AutoCompleteTextView(this);
        actv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        actv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
        actv.setThreshold(1);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView(actv);

        setContentView(ll);
    }
}

除了不直观(后退键提示暗示将返回到键盘)之外,导航AutoCompleteTextView 建议非常烦人。

什么是最少侵入性的方式(例如在每个活动中抓住onBackPressed()并相应地路由它肯定不是理想的)使第一次后按隐藏键盘,以及第二个删除建议列表?

【问题讨论】:

  • 您找到解决方案了吗?

标签: android keyboard android-softkeyboard autocompletetextview onbackpressed


【解决方案1】:

您可以通过在自定义 AutoCompleteTextView 中覆盖 onKeyPreIme 来实现。

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

    public CustomAutoCompleteTextView(Context context) {
        super(context);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {
            InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            
            if (inputManager.hideSoftInputFromWindow(findFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS)) {    
                return true;
            }
        }

        return super.onKeyPreIme(keyCode, event);
    }

}

【讨论】:

  • 很好的解决方案,但似乎不起作用。键被正确抓取和识别(即 hideSoftInputFromWindow 被触发),但键盘实际上并没有消失。是否会与 EditText 中的某些内容发生冲突,该内容会在视图获得焦点时保持软键盘向上?
  • 刚刚在空白项目中尝试了它,只有问题中的代码,它可以工作。在我更复杂的例子中,一定有一些与此冲突的东西。尽管如此,感谢您提供的简单的解决方案!
  • 你成就了我的一天。非常感谢!
  • Awsomeeeeeeeee!!
  • MashaAllah,非常棒,在 Android 10 中工作
【解决方案2】:

这样设置DismissClickListener

 autoCompleteTextView.setOnDismissListener(new AutoCompleteTextView.OnDismissListener() {
            @Override
            public void onDismiss() {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);
            }
        });

【讨论】:

  • 这仅在 API17 发布后有效。 API
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 2011-08-29
  • 2011-05-25
  • 2023-03-24
相关资源
最近更新 更多