【问题标题】:Android EditText keyboard not shown in Fragment片段中未显示 Android EditText 键盘
【发布时间】:2015-11-19 02:38:27
【问题描述】:

我在Fragment 中添加了EditText,但即使我点击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>

如果我使用&lt;requestFocus /&gt;,则显示键盘,但它仅适用于EditText 之一。我能怎么做?为什么不显示键盘?

【问题讨论】:

  • 你应该不需要任何额外的代码来在编辑文本触摸时显示键盘,你可以发布类文件吗?
  • @Skynet 我也认为没有必要采取任何行动 T.T.。另外,我没有在我的类文件中做任何事情。
  • @Skynet 只需在onCreateView 中设置View rootView = inflater.inflate(R.layout.setting, container, false);return rootView;
  • 我怀疑其他东西正在占据您的点击焦点。
  • 你有解决方案吗?我也有同样的问题

标签: android android-layout android-fragments android-edittext android-softkeyboard


【解决方案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);

【讨论】:

    【解决方案2】:

    我想建议您调用您的代码:

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

    通过实现 EditText Touch listner 方法,您得到的结果如下:

    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
    • 您是否遇到任何错误?
    • 如果您在 EditText 上添加了任何其他触摸事件,请尝试添加上述代码并删除,因为我已经这样做了。
    • 但是我已经在我的代码之上了。我只应用你的代码,但它不起作用.. T.T
    【解决方案3】:

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

     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时显示键盘
    • 我刚刚更新了我的答案,请尝试延迟一些时间。在上面的代码中,我给出了 1000 毫秒。
    【解决方案4】:

    试试这个..

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

    希望这会有所帮助...

    【讨论】:

      猜你喜欢
      • 2013-08-23
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2015-09-29
      • 2019-03-21
      相关资源
      最近更新 更多