【问题标题】:Keyboard auto on in AndroidAndroid中的键盘自动开启
【发布时间】:2016-11-29 14:50:56
【问题描述】:

我想在 Android 应用中实现一个平板电脑,它会自动在屏幕上显示键盘。现在我的情况是我在 xml 文件中添加了一个编辑文本,当单击编辑文本时,键盘出现,我想显示键盘自动打开。 当用户单击复选标记时,键盘消失,但我希望键盘不会被关闭,单击复选标记意味着检查输入。 任何样品或帮助表示赞赏!

【问题讨论】:

  • @这里尝试添加一些代码以便我们看清楚
  • 对不起,Firefog,我还没有写那个代码,我会尝试其他人提供的方法,谢谢!

标签: android xml keyboard


【解决方案1】:

在活动中使用以下方法

//To show the keyboard
public void showKeyboard(Activity activity) {
    if(activity.getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
    }
}

//To hide the keyboard
public void hideKeyboard(Activity activity) {
        if(activity.getCurrentFocus()!=null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }

在 EditText 中使用下面的方法

//To show the keyboard
public void showSoftKeyboard(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }

//To hide the keyboard
    `public void hideSoftKeyboard(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

根据您的要求使用上述方法

【讨论】:

    【解决方案2】:

    这会起作用,并且总是尝试使用 etText.requestfocus(); 它会自动打开一个键盘 和 public static void ShowKeyboard(Activity activity, View v) {

    InputMethodManager inputMethodManager =
                (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(
            view.getApplicationWindowToken(),
            InputMethodManager.SHOW_FORCED, 0);
    

    }

    【讨论】:

    • 谢谢大家的回答,伙计们!没有更清楚地描述问题是我的错误。我想创建一个对话框,并且对话框中有一个edittext,我希望在对话框显示时自动打开软键盘。我找到了解决方法,这是我的代码: Dialog dialog = new MyDialog(getActivity()); dialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);对话框.show();
    • 哦,这真是太棒了!继续加油!
    【解决方案3】:

    检查这个: 将此添加到您的复选框上单击

    显示软键盘:

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

     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    

    隐藏软键盘:

     InputMethodManager imm = InputMethodManager)
    getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(FOCUSABLE_VIEW.getWindowToken(), 0);
    
    Here, “FOCUSABLE_VIEW” can be any view which is visible on screen like
    

    参考:http://chintanrathod.com/show-hide-soft-keyboard-programmatically-in-android/

    【讨论】:

      【解决方案4】:

      以下是显示和隐藏键盘的方法。看看吧。

      public static void ShowKeyboard(Activity activity, View view) {
          InputMethodManager inputMethodManager =
                  (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputMethodManager.toggleSoftInputFromWindow(
                  view.getApplicationWindowToken(),
                  InputMethodManager.SHOW_FORCED, 0);
      }
      
      public static void HideKeyBoard(Activity activity) {
          try {
              InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
              if (inputMethodManager.isAcceptingText())
                  inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      

      如果你想在activity开始时打开键盘,你可以在manifest的<activity>标签中添加如下代码:

      android:windowSoftInputMode="stateVisible"
      

      这将在您开始活动时显示键盘。

      【讨论】:

        【解决方案5】:

        你可以使用下面的代码,

        edittext.requestfocus();

        这会自动带上键盘。

        不要使用, 安卓:windowSoftInputMode="stateHidden" 在您的 android 清单文件中...

        只需从您的 Activity 中删除“android:windowSoftInputMode”即可。

        【讨论】:

        • 对不起,editext.requestfocus();剂量对我不起作用。这是我的代码: LinearLayout linearLayout=(LinearLayout)inflater.inflate(R.layout.ms_pdf_viewer_support, container, false); EditText editText=(EditText) linearLayout.findViewById(R.id.editText); editText.requestFocus();
        • 不要在您的 Android Manifestfile 中使用以下内容,
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        • 2016-04-16
        相关资源
        最近更新 更多