代码之家  ›  专栏  ›  技术社区  ›  Randy Sugianto 'Yuku'

android:当焦点在edittext上时自动显示软键盘

  •  307
  • Randy Sugianto 'Yuku'  · 技术社区  · 15 年前

    我在显示一个输入框 AlertDialog . 这个 EditText 在对话框内部,当我调用 AlertDialog.show() ,但软键盘不会自动显示。

    如何使软键盘在显示对话框时自动显示?(而且没有物理/硬件键盘)。与按下搜索按钮调用全局搜索类似,软键盘也会自动显示。

    24 回复  |  直到 6 年前
        1
  •  288
  •   Randy Sugianto 'Yuku'    15 年前

    您可以在 EditText AlertDialog ,然后获取 对话框 Window . 从那里你可以通过调用 setSoftInputMode .

    final AlertDialog dialog = ...;
    
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
    
        2
  •  207
  •   horkavlna    12 年前

    显示键盘使用:

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

    隐藏键盘使用:

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(),0); 
    
        3
  •  105
  •   Bao Le    9 年前

    您可以在创建对话框后立即请求软键盘(在sdk-r20上测试)

    // create dialog
    final AlertDialog dialog = ...; 
    
    // request keyboard   
    dialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
        4
  •  24
  •   tidbeck    13 年前

    我有同样的问题,并用下面的代码解决了它。我不确定它在带有硬件键盘的手机上会有什么表现。

    // TextEdit
    final EditText textEdit = new EditText(this);
    
    // Builder
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Enter text");
    alert.setView(textEdit);
    
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String text = textEdit.getText().toString();
            finish();
        }
    });
    
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    
    // Dialog
    AlertDialog dialog = alert.create();
    dialog.setOnShowListener(new OnShowListener() {
    
        @Override
        public void onShow(DialogInterface dialog) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(textEdit, InputMethodManager.SHOW_IMPLICIT);
        }
    });
    
    dialog.show();
    
        5
  •  22
  •   Peter O. Manuel Pinto    13 年前

    我发现了这个例子 http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html . 在前面添加以下代码 alert.show() .

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    
        6
  •  16
  •   ahtartam    11 年前
    <activity
        ...
        android:windowSoftInputMode="stateVisible" >
    </activity>
    

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
        7
  •  13
  •   Soren Stoutner    9 年前

    来自其他答案的代码片段可以工作,但在代码中的位置并不总是很明显,特别是在使用 AlertDialog.Builder 然后跟着 official dialog tutorial 因为它没用 final AlertDialog ... alertDialog.show() .

    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    

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

    因为软输入状态总是可见的,所以如果焦点从编辑文本切换到非编辑文本,则会隐藏键盘,在编辑文本中强制显示将保持键盘显示,直到键盘被显式关闭,即使用户返回主屏幕或显示最近的应用程序。

    下面是使用XML定义的EditText自定义布局创建的AlertDialog的工作代码。它还设置键盘有一个“go”键,并允许它触发正按钮。

    警报对话框.xml:

    <RelativeLayout
    android:id="@+id/dialogRelativeLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    
        <!-- android:imeOptions="actionGo" sets the keyboard to have a "go" key instead of a "new line" key. -->
        <!-- android:inputType="textUri" disables spell check in the EditText and changes the "go" key from a check mark to an arrow. -->
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="16dp"
            android:imeOptions="actionGo"
            android:inputType="textUri"/>
    
    </RelativeLayout>
    

    AlertDialog.java:

    import android.app.Activity;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.v4.app.DialogFragment;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatDialogFragment;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.EditText;
    
    public class CreateDialog extends AppCompatDialogFragment {
        // The public interface is used to send information back to the activity that called CreateDialog.
        public interface CreateDialogListener {
            void onCreateDialogCancel(DialogFragment dialog);    
            void onCreateDialogOK(DialogFragment dialog);
        }
    
        CreateDialogListener mListener;
    
        // Check to make sure that the activity that called CreateDialog implements both listeners.
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mListener = (CreateDialogListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement CreateDialogListener.");
            }
        }
    
        // onCreateDialog requires @NonNull.
        @Override
        @NonNull
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
            LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
    
            // Setup dialogBuilder.
            alertDialogBuilder.setTitle(R.string.title);
            alertDialogBuilder.setView(customDialogInflater.inflate(R.layout.alert_dialog, null));
            alertDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mListener.onCreateDialogCancel(CreateDialog.this);
                }
            });
            alertDialogBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mListener.onCreateDialogOK(CreateDialog.this);
                }
            });
    
            // Assign the resulting built dialog to an AlertDialog.
            final AlertDialog alertDialog = alertDialogBuilder.create();
    
            // Show the keyboard when the dialog is displayed on the screen.
            alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
            // We need to show alertDialog before we can setOnKeyListener below.
            alertDialog.show();
    
            EditText editText = (EditText) alertDialog.findViewById(R.id.editText);
    
            // Allow the "enter" key on the keyboard to execute "OK".
            editText.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button, select the PositiveButton "OK".
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        // Trigger the create listener.
                        mListener.onCreateDialogOK(CreateDialog.this);
    
                        // Manually dismiss alertDialog.
                        alertDialog.dismiss();
    
                        // Consume the event.
                        return true;
                    } else {
                        // If any other key was pressed, do not consume the event.
                        return false;
                    }
                }
            });
    
            // onCreateDialog requires the return of an AlertDialog.
            return alertDialog;
        }
    }
    
        8
  •  9
  •   sberezin    9 年前

    好吧,这是一个很老的帖子,仍然有一些东西要补充。
    以下两种简单的方法可以帮助我控制键盘,它们的工作非常完美:

    显示键盘

    public void showKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        View v = getCurrentFocus();
        if (v != null)
            imm.showSoftInput(v, 0);
    }
    

    隐藏键盘

    public void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        View v = getCurrentFocus();
        if (v != null)
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    
        9
  •  8
  •   resueman    9 年前

    让我指出一些额外的信息,以解决Yuku,因为我发现这很难得到工作!如何从alertDialog.builder中获取alertDialog对象?嗯,这是我 alert.show() 执行:

    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(getActivity());
    alert.setView(input);
    
    // do what you need, like setting positive and negative buttons...
    
    final AlertDialog dialog = alert.show();
    
    input.setOnFocusChangeListener(new OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
          if(hasFocus) {
             dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
          }
       }
    });
    
        10
  •  7
  •   jqpubliq    15 年前

    看一看 this 处理手动隐藏和显示输入法的讨论。然而,我的感觉是如果 EditText 不是因为你打电话来 AlertDialog.show() 在你 OnCreate() 或者其他一些在屏幕实际出现之前被唤醒的方法。把它移到 OnPostResume() 如果那样的话,我相信会解决的。

        11
  •  5
  •   MKJParekh    12 年前

    是的,你可以 setOnFocusChangeListener 它会帮助你的。

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
    
        12
  •  3
  •   Ryan Wittenburg    12 年前

    如果有人得到:

    无法从类型活动对非静态方法getSystemService(字符串)进行静态引用

    尝试添加 语境 获取系统服务调用。

    所以

    InputMethodManager imm = 
    (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    
        13
  •  1
  •   Timo Dylan Markow    10 年前

    最初的问题是关于对话框,而我的edittext是在常规视图上。不管怎样,我想这对你们大多数人也应该有效。这就是对我有效的方法(上面建议的最高评级方法对我没有任何帮助)。这是一个定制的editview(子类化是不必要的,但我发现它对我的目的很方便,因为我想在视图可见时也抓住焦点)。

    这实际上与tidbecks的答案基本相同。实际上我根本没注意到他的答案,因为他的答案是零票。然后我正要评论他的文章,但这篇文章太长了,所以无论如何我结束了这篇文章。tidbeck指出,他不确定它如何在有键盘的设备上工作。我可以证实,在这两种情况下,他们的行为似乎完全相同。在纵向模式下,软件键盘会弹出,而在横向模式下则不会。在我的手机上,物理键盘滑出或不滑出都没有区别。

    因为,我个人觉得我选择使用的行为有点尴尬: InputMethodManager.SHOW_FORCED . 这是我想要的工作。不管方向如何,键盘都会显示出来,但至少在我的设备上,如果硬件键盘滑出,它不会弹出。

    import android.app.Service;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;
    
    public class BringOutTheSoftInputOnFocusEditTextView extends EditText {
    
        protected InputMethodManager inputMethodManager;
    
        public BringOutTheSoftInputOnFocusEditTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        public BringOutTheSoftInputOnFocusEditTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public BringOutTheSoftInputOnFocusEditTextView(Context context) {
            super(context);
            init();
        }
    
        private void init() {
            this.inputMethodManager = (InputMethodManager)getContext().getSystemService(Service.INPUT_METHOD_SERVICE);
            this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        BringOutTheSoftInputOnFocusEditTextView.this.inputMethodManager.showSoftInput(BringOutTheSoftInputOnFocusEditTextView.this, InputMethodManager.SHOW_FORCED);
                    }
                }
            });
        }
    
        @Override
        protected void onVisibilityChanged(View changedView, int visibility) {
            super.onVisibilityChanged(changedView, visibility);
            if (visibility == View.VISIBLE) {
                BringOutTheSoftInputOnFocusEditTextView.this.requestFocus();
            }
        }
    
    }
    
        14
  •  1
  •   Allan Veloso Vineeth Reddy    10 年前

    问题似乎是,由于输入文本的地方最初是隐藏的(或嵌套的或其他),alertdialog会自动设置标志 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 这样就不会触发软输入来显示。

    解决此问题的方法是添加以下内容:

    (...)
    // Create the dialog and show it
    Dialog dialog = builder.create()
    dialog.show();
    
    // After show (this is important specially if you have a list, a pager or other view that uses a adapter), clear the flags and set the soft input mode
    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
        15
  •  1
  •   ungalcrys    10 年前

    尝试使用:

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
        16
  •  1
  •   EpicPandaForce Jigar Joshi    6 年前

    为了显示键盘,对我来说,我必须做以下工作

    Android TextField : set focus + soft input programmatically

    基本上解决方法如下

    @Override
    public void onResume() {
        super.onResume();
        //passwordInput.requestFocus(); <-- that doesn't work
        passwordInput.postDelayed(new ShowKeyboard(), 325); //250 sometimes doesn't run if returning from LockScreen
    }
    

    在哪里? ShowKeyboard

    private class ShowKeyboard implements Runnable {
        @Override
        public void run() {
            passwordInput.setFocusableInTouchMode(true);
            //passwordInput.requestFocusFromTouch(); //this gives touch event to launcher in background -_-
            passwordInput.requestFocus();
            getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
        }
    }
    

    输入成功后,我还要确保隐藏键盘

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(getView().getWindowToken(), 0);
    
        17
  •  1
  •   i am E Mahdi    6 年前

    我创建了很好的kotlin esqe扩展函数,以防有人感兴趣

    fun Activity.hideKeyBoard() {
        val view = this.currentFocus
        val methodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        assert(view != null)
        methodManager.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
    
    fun Activity.showKeyboard() {
        val view = this.currentFocus
        val methodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        assert(view != null)
        methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
    
        18
  •  0
  •   A.A    10 年前

    这是一个很好的样品:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ScrollView
            android:id="@+id/scrollID"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >
    
            <LinearLayout
                android:id="@+id/test"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="true"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:weightSum="1" >
    
            <EditText
                android:id="@+id/txtInpuConversation"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:hint="@string/edt_Conversation" >
    
                <requestFocus />
            </EditText>
    
            <Button
                android:id="@+id/btnSend"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="@string/btn_Conversation" />
        </LinearLayout>
    
    </LinearLayout>
    
        19
  •  0
  •   Darpan    10 年前

    为什么这个答案-因为上面的解决方案将显示您的键盘,但如果您单击其他任何地方,它不会消失 EditText . 所以你得做点什么让凯宝路消失 编辑文本 失去焦点。

    您可以通过执行以下步骤来实现此目的:

    1. 通过添加以下属性,使父视图(活动的内容视图)可单击且可聚焦

          android:clickable="true" 
          android:focusableInTouchMode="true" 
      
    2. 实现hidekeyboard()方法

          public void hideKeyboard(View view) {
              InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
              inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),InputMethodManager.HIDE_IMPLICIT_ONLY );
          }
      
    3. 最后,设置EditText的onFocusChangeListener。

          edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
              @Override
              public void onFocusChange(View v, boolean hasFocus) {
                  if (!hasFocus) {
                      hideKeyboard(v);
                  }
              }
          });
      
        20
  •  0
  •   FRR    10 年前

    这有点棘手。我是这样做的,而且成功了。

    1.在第一次调用时隐藏窗口中的软输入。如果软键盘可见,这将隐藏软输入;如果不可见,则不执行任何操作。

    2.显示对话框

    3.然后简单调用切换软输入。

    代码:

    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    //hiding soft input
    inputManager.hideSoftInputFromWindow(findViewById(android.R.id.content).getWind‌​owToken(), 0);
    //show dialog
    yourDialog.show();
    //toggle soft input
    inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.SHOW_IMPLICIT);
    
        21
  •  0
  •   GameBug    6 年前

    试试这个

    someutils.java语言

    public static void showKeyboard(Activity activity, boolean show) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    
        if(show)
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        else
            inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
    }
    
        22
  •  0
  •   Khemraj Sharma    6 年前

    将这些方法放在util类中并在任何地方使用。

    科特林

    fun hideKeyboard(activity: Activity) {
        val view = activity.currentFocus
        val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        assert(view != null)
        methodManager.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
    
    private fun showKeyboard(activity: Activity) {
        val view = activity.currentFocus
        val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        assert(view != null)
        methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
    

    爪哇

    public static void hideKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert methodManager != null && view != null;
        methodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    
    private static void showKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert methodManager != null && view != null;
        methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
    
        23
  •  0
  •   CoolMind    6 年前

    AS horkavlna 写的,

    切换

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

    隐藏 键盘

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    

    方法有效。但是 显示 变体在我的情况下不起作用。所以在 onCreate() 我放 hideKeyboard(editText); 然后在 onStart() 我写 toggleKeyboard(editText); 而在 onStop() 我写 隐藏键盘(编辑文本); .

    有三个问题:

    1)当应用程序以关闭屏幕启动时,它将隐藏键盘,

    2)每次你打开屏幕,它都会显示键盘,

    3)应用程序完成后,您可以在Android主屏幕上看到键盘。

    经过几次测试,我删除了这些方法 AndroidManifest 在里面 activity 标签写道 android:windowSoftInputMode="stateVisible" android:windowSoftInputMode="stateAlwaysHidden" .

        24
  •  -1
  •   atline    6 年前
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    

    我在oncreate()中调用这个函数,以便在进入活动时自动显示键盘。