Soft Keyboard doesn't appear when tapping on EditText

时间:2015-06-15 14:24:06

标签: android android-fragments keyboard android-edittext

I got a really weird problem. I am working on an Android app. 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.

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. I tried to force show the keyboard within the onClick() with following code

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. The first one is working as expected.

EDIT2: This is my layout:

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

2 个答案:

答案 0 :(得分:0)

In your xml use, in your EditText tags

<requestFocus />

source : EditText request focus

Also checkout : Android: show soft keyboard automatically when focus is on an EditText

P.S. Little exploration on SO or Google would have the problem, and will save your time of writing the question.

答案 1 :(得分:0)

在逐步重新创建项目后,我发现了我的错误。

在我的第一个片段中,我使用AsyncTask和.get()从服务器获取SID。删除.get()并更改了我的后续逻辑后,一切都按预期工作。