简体   繁体   English

当点击EditText时,软键盘没有出现

[英]Soft Keyboard doesn't appear when tapping on EditText

I got a really weird problem. 我遇到了一个很奇怪的问题。 I am working on an Android app. 我正在开发一个Android应用程序。 If I use my layout on an Activity everything works as expected. 如果我在活动上使用布局,那么一切都会按预期进行。 But, if I use my layout in a Fragment the Soft Keyboard doesn't show anymore when I focus an EditText . 但是,如果我在Fragment中使用布局,则当我聚焦EditText时,软键盘将不再显示。

Here is the code of my fragment: 这是我片段的代码:

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import mariohenkel.com.familycheck.R;
import mariohenkel.com.familycheck.fragment.api.BaseFragment;

/**
 * Created by Developer on 15.06.2015.
 */
public class SuchenFragment  extends BaseFragment {

    public static SuchenFragment newInstance() {
        return new SuchenFragment();
    }

    private View v;
    EditText etOrt;

    public SuchenFragment(){ }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("SuchenFragment", "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_suchen_neu, container, false);

        loadViewComponents();
        loadInfoView();

        return v;
    }

    private void loadViewComponents() {
        etOrt = (EditText) v.findViewById(R.id.editText_Ort);

        etOrt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("onClick", "onClick");

            }
        });
    }

    private void loadInfoView() {

    }
}

The onClick() Event of the EditText fires correctly. EditText的onClick()事件正确触发。 I tried to force show the keyboard within the onClick() with following code 我试图通过以下代码强制在onClick()显示键盘

InputMethodManager mImm = (InputMethodManager) 
                        getSystemService(Context.INPUT_METHOD_SERVICE);  
mImm.showSoftInput(SearchEdit, InputMethodManager.SHOW_IMPLICIT); 

but without success. 但没有成功。 Any ideas what I am doing wrong? 有什么想法我做错了吗?

EDIT: I am using this project https://github.com/halysongoncalves/Material-Design-Example and I am trying to use the second tab. 编辑:我正在使用这个项目https://github.com/halysongoncalves/Material-Design-Example ,我正在尝试使用第二个选项卡。 The first one is working as expected. 第一个按预期方式工作。

EDIT2: This is my layout: EDIT2:这是我的布局:

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

    <TableLayout
        android:id="@+id/tl_views"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
        <TableRow
            android:background="@color/accent"
            android:minHeight="60dp">

            <TextView
                android:id="@+id/titel_ort"
                android:layout_marginLeft="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:textAppearanceLarge"
                android:layout_gravity="center_vertical"
                android:text="Ort: "/>

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:hint="Ort eingeben"
                android:id="@+id/editText_Ort"
                android:paddingRight="16dp">
                <requestFocus />
            </EditText>

        </TableRow>
        <TableRow
            android:background="@color/gruen"
            android:minHeight="60dp">
            <TextView
                android:id="@+id/titel_kategorie"
                android:layout_marginLeft="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:textAppearanceLarge"
                android:layout_gravity="center_vertical"
                android:text="Kategorie: "/>

            <Spinner
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/spinner_Kategorie"
                android:layout_gravity="center_vertical"
                android:paddingRight="16dp"
                android:spinnerMode="dropdown" />

        </TableRow>
        <TableRow
            android:background="@color/gelb"
            android:minHeight="60dp">
            <TextView
                android:id="@+id/titel_suchbegriff"
                android:layout_marginLeft="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:textAppearanceLarge"
                android:layout_gravity="center_vertical"
                android:text="Suchbegriff: "/>

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText_Suchbegriff"
                android:layout_gravity="center_vertical"
                android:hint="Suchbegriff"
                android:paddingRight="16dp"/>
        </TableRow>
    </TableLayout>
    <RelativeLayout
        android:layout_below="@+id/tl_views"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/theme_dialer_primary"
        android:gravity="center">
        <TextView
            android:id="@+id/titel_suchen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textAppearance="?android:textAppearanceLarge"
            android:text="Jetzt suchen"/>
    </RelativeLayout>

</RelativeLayout>

Update: One more weird thing. 更新:另一件事很奇怪。 If I open another activity and go back to the activity which hosts the fragments the keyboard is working as expected... 如果我打开另一个活动并返回到托管片段的活动,则键盘正在按预期方式工作...

In your xml use, in your EditText tags 在您的xml使用中,在您的EditText标签中

<requestFocus />

source : EditText request focus 来源: EditText请求焦点

Also checkout : Android: show soft keyboard automatically when focus is on an EditText 也结帐: Android:当焦点位于EditText上时自动显示软键盘

PS Little exploration on SO or Google would have the problem, and will save your time of writing the question. PS对SO或Google进行很少的探索会遇到问题,并将节省您编写问题的时间。

After recreating my project step by step I found my mistake. 逐步重新创建项目后,我发现了自己的错误。

In my first fragment I used an AsyncTask with .get() to aquire a SID from a server. 在我的第一个片段中,我使用了带有.get()的AsyncTask来从服务器获取SID。 After I removed that .get() and changed my following logic everything worked as expected. 在删除该.get()并更改了以下逻辑之后,所有操作均按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM