Android EditText在Fragment中无法显示键盘

7

我在Fragment中添加了EditText, 但是即使我点击EditText, 键盘也不会显示出来。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <EditText
        android:id="@+id/setting_nickname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:gravity="right"
        android:hint="Samantha"
        android:textColorHint="@color/nav_selected"
        android:textSize="14dp"
        android:textColor="@color/nav_selected"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >
    </EditText>

    <EditText
        android:id="@+id/setting_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:gravity="right"
        android:hint="Samantha"
        android:textColorHint="@color/nav_selected"
        android:textSize="14dp"
        android:textColor="@color/nav_selected"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >
    </EditText>

</LinearLayout>

我尝试获取EditText的监听器

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
edit_nickname.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        imm.showSoftInput(edit_nickname, 0);

        //edit_nickname.requestFocus();  --> fail
        //imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_IMPLICIT);  --> fail
        //imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_FORCED);  --> fail
    }
});

这段代码未能成功...

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

此外,我尝试添加LinearLayout以实现focusableInTouchModefocusable,但失败了。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:background="@color/black_transparent"
    android:focusableInTouchMode="true"
    android:focusable="true">
    <EditText .../>
    <EditText .../>
</LinearLayout>

如果我使用<requestFocus />,那么键盘会弹出,但仅适用于EditText中的一个。我该怎么办?为什么键盘不显示?

您不需要任何额外的代码来显示编辑文本触摸键盘,您能发布类文件吗? - Skynet
@Skynet 我也认为不需要采取任何行动 T.T. 此外,我在我的类文件中什么都没做。 - Soyeon Kim
@Skynet 只需要在 onCreateView 中设置 View rootView = inflater.inflate(R.layout.setting, container, false);return rootView; - Soyeon Kim
我个人怀疑您的点击被其他事情所吸引。 - Skynet
1
你解决了吗?我也遇到了同样的问题。 - Muhammad chhota
我曾经遇到过类似的问题,并找到了解决方法:https://dev59.com/WGw05IYBdhLWcg3w3lkl#56097616 - ban-geoengineering
4个回答

1
尝试使用这个,它对我有效。
EditText myEd= (EditText)getActivity().findViewById(R.id.myEd);
myEd.requestFocus(); 
InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(myEd, InputMethodManager.SHOW_IMPLICIT);

0

尝试在单独的线程或处理程序中执行以下代码。

 editText.postDelayed(new Runnable() {
                @Override
                public void run() {

                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if(imm != null)
                    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            },1000);

谢谢!但是这段代码仅在创建片段时发生。我想要在焦点集中于“EditText”时显示键盘。 - Soyeon Kim
我刚刚更新了我的回答,请尝试添加时间延迟。在上面的代码中,我已经给出了1000毫秒的延迟。 - suresh n

0

我想建议您将您的代码命名为:

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(edit_nickname, 0);

通过实现EditText的Touch监听方法,您可以得到以下结果:
edit_nickname.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(edit_nickname, 0);


            return false;
        }
    });

谢谢,我已经按照你的回答尝试了同样的方法,但对我来说没有起作用。T.T - Soyeon Kim
你有收到任何错误吗? - Rajan Bhavsar
请尝试添加上述代码,并删除如果您已经在EditText上添加了任何其他触摸事件,因为我已经完成了那部分。 - Rajan Bhavsar
但是我已经检查过我的代码了。我只是应用了你的代码,但它没有起作用... T.T - Soyeon Kim

0

试试这个...

private InputMethodManager mInputMethodManager;

EditText mRecordFileNameET = (EditText) findViewById(R.id.recorder_recordFileName_et);
mRecordFileNameET.setOnKeyListener(this);

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.recorder_recordFileName_tv:
        mRecordFileNameET.requestFocus();
        showSoftKeyboard(true);
        break;
    }

}

//this method is used to show and hide soft keyboard
private void showSoftKeyboard(boolean showkeyboard) {
    if (showkeyboard) {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.showSoftInput(mRecordFileNameET, InputMethodManager.SHOW_IMPLICIT);
    } else {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(mRecordFileNameET.getWindowToken(), 0);
    }

}

希望这能有所帮助...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接