ذكري المظهر:إظهار لوحة المفاتيح الناعمة تلقائيًا عندما يكون التركيز على تحرير النص

StackOverflow https://stackoverflow.com/questions/2403632

سؤال

أعرض مربع الإدخال باستخدام AlertDialog.ال EditText يتم التركيز تلقائيًا داخل مربع الحوار نفسه عند الاتصال AlertDialog.show(), ، ولكن لا يتم عرض لوحة المفاتيح الإلكترونية تلقائيًا.

كيف أجعل لوحة المفاتيح الإلكترونية تظهر تلقائيًا عند ظهور مربع الحوار؟(ولا توجد لوحة مفاتيح فعلية/أجهزة).تمامًا كما هو الحال عندما أضغط على زر البحث لاستدعاء البحث الشامل، تظهر لوحة المفاتيح الإلكترونية تلقائيًا.

هل كانت مفيدة؟

المحلول

يمكنك إنشاء مستمع التركيز على EditText على ال AlertDialog, ، ثم الحصول على AlertDialogWindow. وبعد من هناك يمكنك أن تجعل لوحة المفاتيح الناعمة تظهر عن طريق الاتصال 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);
        }
    }
});

نصائح أخرى

لإظهار استخدام لوحة المفاتيح:

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); 

يمكنك طلب لوحة مفاتيح لينة مباشرة بعد إنشاء مربع الحوار (اختبار على SDK - R20)

// create dialog
final AlertDialog dialog = ...; 

// request keyboard   
dialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

كان لدي نفس المشكلة وحلها مع التعليمات البرمجية التالية. لست متأكدا من كيفية التصرف على هاتف مع لوحة مفاتيح الأجهزة.

// 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();

لقد وجدت هذا المثال http://android-codes-examples.blogspot.com/logpot.com/11/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);
<activity
    ...
    android:windowSoftInputMode="stateVisible" >
</activity>

أو

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

مقتطفات من التعليمات البرمجية من الإجابات الأخرى العمل، ولكن ليس من الواضح دائما أين تضعها في التعليمات البرمجية، خاصة إذا كنت تستخدم AlertDialog.Builder وتبعت البرنامج التعليمي الرسمي لأنه لا يستخدم 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);

نظرا لأن Soft_Input_State_Always_Visible سوف تخفي لوحة المفاتيح إذا كان التركيز يبتعد عن EDITTEXT، حيث سيتم عرض لوحة المفاتيح المعروضة على لوحة المفاتيح حتى يتم رفضها بشكل صريح، حتى إذا عرج المستخدم إلى HomeScreen أو يعرض التطبيقات الأخيرة.

أدناه هو رمز العمل ل AlertDialog تم إنشاؤه باستخدام تخطيط مخصص مع تعريف Edittext في XML. كما أنه يضع لوحة المفاتيح للحصول على مفتاح "GO" ويسمح له بتشغيل الزر الإيجابي.

Alert_Dialog.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;
    }
}

حسنا، هذا هو وظيفة قديمة جدا، لا يزال هناك شيء لإضافته.
هذه هي طريقتان بسيطة تساعدني في الحفاظ على التحكم في لوحة المفاتيح وأنها تعمل تماما:

إظهار لوحة المفاتيح

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);
}

اسمحوا لي أن أشير إلى بعض المعلومات الإضافية لحل Yuku، لأنني وجدت صعوبة في الحصول على هذا العمل! كيف أحصل على كائن Alertdialog من Alertdialog.builder؟ حسنا، إنها نتيجة لي 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);
      }
   }
});

إلقاء نظرة على هذه مناقشة التي تتعامل مع الاختباء يدويا وإظهار IME. ومع ذلك، شعوري هو أنه إذا تركز EditText لا يجلب IME هو لأنك تتصل AlertDialog.show() في الخاص بك OnCreate() أو بعض الطريقة الأخرى التي تثيرت قبل تقديم الشاشة بالفعل. نقلها إلى OnPostResume() يجب إصلاحه في هذه الحالة أعتقد.

نعم يمكنك القيام به 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);
        }
    }
});

إذا كان أي شخص يحصل على:

لا يمكن إجراء مرجع ثابت إلى الطريقة غير الثابتة GetSystemService (سلسلة) من نشاط النوع

حاول إضافة سياق الكلام إلى getsystemservice دعوة.

وبالتالي

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

المسألة الأصلية يتعلقة الحوار وبلدي 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();
        }
    }

}

يبدو أن المشكلة هي أنه نظرا لأن المكان الذي تدخل فيه النص مخفي مبدئيا (أو متداخلا أو شيءا)، فإن 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);

حاول واستخدام:

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

لإظهار لوحة المفاتيح، بالنسبة لي، اضطررت للقيام بما يلي

Android Textfield: Set Focus + إدخال لينة برمجيا

في الأساس الحل هو ما يلي

@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);

ضع هذه الأساليب في فئة 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);
}

لقد أنشأت وظائف تمديد 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)
}

هذه هي عينة جيدة بالنسبة لك:

<?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>

لماذا هذه الإجابة - لأن الحل أعلاه سيظهر لوحة المفاتيح الخاصة بك ولكنها لن تختفي إذا قمت بالنقر فوق أي مكان آخر EditText. وبعد لذلك عليك أن تفعل شيئا لجعل Keybaord تختفي متى 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. أخيرا، اضبط onfocuschangelistener من Edittext الخاص بك.

        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    hideKeyboard(v);
                }
            }
        });
    

هذا أمر صعب بعض الشيء.فعلت بهذه الطريقة ونجحت.

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);

جرب هذا

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);
}

حاول الكثيرون ولكن هذا هو ما عمل لي (كوتين):

        val dialog = builder.create()
        dialog.setOnShowListener {
            nameEditText.requestFocus()
            val s = ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
            s?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
        }

        dialog.setOnDismissListener {
            val s = ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
            s?.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
        }

        dialog.show()

انظر الى https://stackoverflow.com/a/39144104/2914140. أنا مبسطة قليلا:

// In onCreateView():
view.edit_text.run {
    requestFocus()
    post { showKeyboard(this) }
}

fun showKeyboard(view: View) {
    val imm = view.context.getSystemService(
        Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}

أنه أفضل من https://stackoverflow.com/a/11155404/2914140.:

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

لأنه عند الضغط على زر الصفحة الرئيسية والانتقال إلى الشاشة الرئيسية، ستبقى لوحة المفاتيح مفتوحة.

أعرف أن هذا السؤال قديم من قبل أعتقد أن استخدام وظيفة التمديد هو وسيلة أجمل لإظهار لوحة المفاتيح لنص تحرير

هنا هي الطريقة التي أستخدمها لإظهار لوحة المفاتيح ل Edittext.

كود كوتين:فقط بحاجة إلى الاتصال edittext.showKeyboard()

fun EditText.showKeyboard() {
  post {
    requestFocus()
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
  }
}

رمز Java:

public static void showKeyboard(EditText editText) {
    editText.post(new Runnable() {
      @Override
      public void run() {
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) editText.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
      }
    });
  }
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

أدعو هذا في OnCreate () لإظهار لوحة المفاتيح تلقائيا، عندما جئت في النشاط.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top