繁体   English   中英

Android-对话框片段:始终隐藏虚拟键盘

[英]Android - Dialog fragment: always hide virtual keyboard

我有一个自定义对话框,它是一个DialogFragment。 这个对话框有一个EditText和我自己的键盘视图,所以我不想使用默认的虚拟键盘。 每当用户触摸EditText时,我都会隐藏虚拟键盘:

edtAmount.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);

        View view = this.getDialog().getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(),0);

        }
        return true;
    }
});

但是由于系统仍然调用虚拟键盘来显示(之前被迫隐藏它),因此系统非常快速地上下移动对话框。 不是很好。

有人可以帮助我避免像这样弹出对话框,只是保持它静止不动吗?

PS:我在清单中尝试过:

android:windowSoftInputMode="adjustNothing" 

但是似乎不起作用。

非常感谢你。

编辑我想保留光标,以便在此线程中找到解决方案: https : //stackoverflow.com/a/14184958/2961402

希望对您有所帮助。

仅当您从自定义EditText扩展了EditText时,才能完成此操作,请对自定义EditText使用以下代码,它们永远不会打开软键盘...!

public class DisableSoftKeyBoardEditText extends EditText {
  public DisableSoftKeyBoardEditText(Context context, AttributeSet attrs) { 
  super(context, attrs);     
  }      
  @Override      
  public boolean onCheckIsTextEditor() {   
  return false;     
  }        
  } 

试试这个代码。 在我的应用程序中,它运行完美

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM