Niestandardowy EditText nie wyświetla klawiatury na ostrości

Tworzę niestandardową klasę EditText, ponieważ muszę ustawić kilka niestandardowych czcionek; jednak teraz, gdy klikam na editText, klawiatura Androida już nie wyskakuje...

Oto moja klasa:

    package ro.gebs.captoom.utils.fonts;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import ro.gebs.captoom.R;

public class CustomFontEditText extends EditText {

    private Context context;

    public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode()) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontEditText,
                    defStyle, 0);

            assert a != null;
            int fontId = a.getInteger(R.styleable.CustomFontEditText_fontNameEdit, -1);
            if (fontId == -1) {
                throw new IllegalArgumentException("The font_name attribute is required and must refer "
                        + "to a valid child.");
            }
            a.recycle();
            initialize(fontId);
        }
        this.context = context;
    }

    public CustomFontEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
    }

    public CustomFontEditText(Context context) {
        super(context);
        this.context = context;
    }

    @SuppressWarnings("ConstantConditions")
    public void initialize(int fontId) {

        Typeface tf = null;
        switch (fontId) {
            case 0:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Regular.ttf");
                break;
            case 1:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Bold.ttf");
                break;
            case 2:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Semibold.ttf");
                break;
            case 3:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-ExtraBold.ttf");
                break;
        }

        setTypeface(tf);
    }
}

I jak go używam w XML:

<ro.gebs.captoom.utils.fonts.CustomFontEditText
                        android:id="@+id/add_details_txt_edit"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:hint="@string/type_here"
                        android:inputType="textPersonName"
                        custom:fontNameEdit="Regular" />

Myślałem, że wydarzenia skupiające są obsługiwane przez fakt, że przedłużam klasę EditText...

Jakieś wskazówki?
Author: Adrian Olar, 2013-09-17

6 answers

To stare pytanie, ale jeśli kogoś to obchodzi, problem tkwi w implementacji konstruktora:

public CustomFontEditText(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    this.context = context;
}

Ostatni argument ("defStyle"), który ustawisz jako 0, powinien być domyślnym stylem dla EditText. Jeśli spojrzysz na ten sam konstruktor w klasie EditText:

public EditText(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.editTextStyle);
}

Jak widzisz, domyślny styl dla EditText powinien być użyty, więc twój konstruktor powinien wyglądać tak:

public CustomFontEditText(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.editTextStyle);
    this.context = context;
}
 96
Author: lluismontero,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-04 08:49:21

Wykonaj Niestandardowy EditText za pomocą Kotlin , aby rozwiązać problem z fokusem:

class MyEditText @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.editTextStyle) : AppCompatEditText(context, attrs, defStyleAttr) {


 init {
    doSomething(context)
 }

 private fun doSomething(context: Context) {
   // TODO Set your custom font or whatever you need
 }

}
 2
Author: Touhid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-23 14:23:14

Dodaj to

 android:focusable="true"
 0
Author: swati srivastav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 11:34:56
implements KeyListener on your custom EditText Class and override methods of KeyListener
 0
Author: Haresh Chhelana,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 11:49:29

Spróbuj odwołać się do edycji tekstu w czasie wykonywania i wywołania żądania focus ()

    et.requestFocus()

I spróbuj

   android:focusable="true"
 0
Author: anddevmanu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 11:50:01
 editText.setOnTouchListener(new OnTouchListener() 
  {
    @Override
    public boolean onTouch(View v, MotionEvent event) 
     {
         editText.setFocusableInTouchMode(true);
         return false;
     }
  });
 -3
Author: Sumant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-17 11:59:34