dimanche 10 mai 2015

Android EditText keyboard not shown in Fragment

I added EditText in Fragment, but my EditText is not showing keyboard even if I click on EditText.

Here is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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>


I try to get listener of 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
    }
});

And this code is fail to...

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

Also, I try to add LinearLayout to have focusableInTouchMode and focusable, but failed.

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


If I use <requestFocus /> than keyboard is shown up but It applies to only one of EditText. How can I do? And why not the keyboard is shown?

Aucun commentaire:

Enregistrer un commentaire