简体   繁体   中英

Soft Keyboard doesn't appear when tapping on 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...

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

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

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. After I removed that .get() and changed my following logic everything worked as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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