【问题标题】:Make EditText not lose focus使 EditText 不会失去焦点
【发布时间】:2018-01-19 13:21:27
【问题描述】:

我在一个页面上有一个 EditText 和一个 ImageButton(在不同的 ViewGroups 中)。

如果 EditText 已聚焦,我不想在单击 ImageButton 时取消聚焦 EditText。

我尝试设置 imageButton.setFocusable(false); 它没有用。 如果我长按 imageButton,EditText 不会失去焦点。

编辑: 我不希望编辑文本失去焦点,即使是一些毫秒也不行。 我的布局取决于 EditText 上的“isFocused”属性。

【问题讨论】:

  • 提供一些代码sn-ps
  • 我不能,我实际上正在使用 xamarin.android + xamarin.forms。但我无法拦截触摸事件,因此它不会聚焦按钮

标签: android android-edittext focus imagebutton long-press


【解决方案1】:

如果你试试这个?

imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            editText.requestFocus();
        }
});

【讨论】:

  • 遗憾的是,这不起作用,因为edittext 仍然会在短时间内失去焦点。当我按下 imageButton 时,“isFocused”根本不应该改变
【解决方案2】:

试试这个代码:

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

【讨论】:

  • 已经有一个答案建议在编辑文本上调用“requestFocus()”。这对我没有帮助,我在其他评论中解释了这个案例
  • 你试过我发布的代码了吗?如果没有,请尝试一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 2013-06-04
  • 1970-01-01
相关资源
最近更新 更多