Custom View EditText for Android and NextFocusDown

In my register activity, there are several custom EditText views set in a vertical LinearLayout. Top down.

Since I need additional functionality for each EditText, I created my own EditText class that provides additional functionality. This class relies on some custom attributes that were added to attrs.xml and retrieved in the class, and then set to the extended EditText.

Everything works fine, except for one thing that I cannot solve. I cannot get NextFocusDown to work with my special EditText views. When using standard EditText views, it works fine. When I switch to my own EditText, it ignores it. All other attributes function normally except this.

What am I missing? Could anyone use this attribute in a custom text editor?

Thanks!

EDIT:

This is part of the custom EditText class:

public class CustomEditText extends RelativeLayout {

private LayoutInflater mInflater = null;
private EditText mEditText;
private TextView mHintQuestionMark;
private TextView mHintText;
private Button mButtonClear;

private boolean isExpandView = false;
private boolean clearButtonEnabled = false;
private boolean hintEnabled = false;


public CustomEditText(Context context) {
    super(context);
    initViews();
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    initViews();
    getCustomAttributes(context, attrs);
}

public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initViews();
    getCustomAttributes(context, attrs);
}

private void initViews() {
    mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mInflater.inflate(R.layout.custom_edit_text, this, true);
    mHintText = (TextView) findViewById(R.id.theHint);
    mEditText = (EditText) findViewById(R.id.theEditText);

    mEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            if (clearButtonEnabled){
                if (charSequence.length() > 0){
                    mButtonClear.setVisibility(VISIBLE);
                } else {
                    mButtonClear.setVisibility(INVISIBLE);
                }
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    mHintQuestionMark = (TextView) findViewById(R.id.hintQuestionMark);
    mHintQuestionMark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isExpandView) {
                collapseHeight(mHintText);
            } else {

                // Expand the HINT view downwards
                expandHeight(mHintText);
            }
        }
    });

    mButtonClear = (Button) findViewById(R.id.buttonClear);
    mButtonClear.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            mEditText.setText("");
        }
    });
}

private void getCustomAttributes(Context ctx, AttributeSet attrs){
    TypedArray typedArray = ctx.getTheme().obtainStyledAttributes(attrs, R.styleable.edittext_attrs, 0, 0);
    int firstNameId = 0;
    int lastNameId = 0;

    try{
        mEditText.setInputType(typedArray.getInt(R.styleable.edittext_attrs_android_inputType, EditorInfo.TYPE_TEXT_VARIATION_NORMAL));
        mEditText.setHint(typedArray.getString(R.styleable.edittext_attrs_android_hint));
        mEditText.setNextFocusDownId(typedArray.getInt(R.styleable.edittext_attrs_android_nextFocusDown, -1));
        clearButtonEnabled = typedArray.getBoolean(R.styleable.edittext_attrs_add_clear_button, false);
        hintEnabled = typedArray.getBoolean(R.styleable.edittext_attrs_add_hint_icon, false);
    } finally {
        typedArray.recycle();
    }

    if (hintEnabled){
        mHintQuestionMark.setVisibility(VISIBLE);
    } else {
        mHintQuestionMark.setVisibility(GONE);

    }
}

This is the custom text editing that I am inflating in this class:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/editTextGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <EditText
            android:id="@+id/theEditText"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_weight="1"
            android:background="@drawable/edit_text_rectangle"
            android:maxLength="53"
            android:padding="12dp"
            android:visibility="visible" />

        <Button
            android:id="@+id/buttonClear"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/remove_text_x"
            android:layout_marginLeft="5dp"
            android:visibility="gone" />

    </RelativeLayout>

    <TextView
        android:id="@+id/hintQuestionMark"
        android:layout_width="36dp"
        android:layout_height="52dp"
        android:text="\?"
        android:textColor="#eeeeee"
        android:textSize="30sp"
        android:gravity="center"
        android:layout_gravity="right"
        android:background="#288abf"
        android:visibility="gone" />

</LinearLayout>

<TextView
    android:id="@+id/theHint"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:padding="8dp"
    android:text="@string/txtEmailHintText"
    android:textColor="#eeeeee"
    android:textSize="15sp"
    android:gravity="left"
    android:background="#288abf"
    android:layout_alignLeft="@+id/editTextGroup"
    android:layout_below="@+id/editTextGroup"
    android:visibility="gone" />

</RelativeLayout>

This is the attrs.xml file that I use to get values ​​in a custom edittext class:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="edittext_attrs">

    <attr name="android:inputType" />
    <attr name="android:hint" />
    <attr name="android:nextFocusDown" />
    <attr name="add_clear_button" format="boolean" />
    <attr name="add_hint_icon" format="boolean" />

</declare-styleable>
</resources>

And this is the tiny part of the xml layout that I use for register activity with two views that NextFocusDown should use:

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp">

        <com.name.view.customViews.CustomEditText
            android:id="@+id/registerTxtFirstName"
            android:layout_width="133dp"
            android:layout_height="52dp"
            android:nextFocusDown="@+id/registerTxtLastName"
            android:layout_alignParentLeft="true"
            android:inputType="textPersonName"
            custom:add_clear_button="true"
            android:hint="@string/first"
            android:visibility="visible" />

        <com.name.view.customViews.CustomEditText
            android:id="@+id/registerTxtLastName"
            android:layout_width="133dp"
            android:layout_height="52dp"
            android:layout_alignParentRight="true"
            custom:add_clear_button="true"
            android:hint="@string/last"
            android:inputType="textPersonName"
            android:visibility="visible" />


    </RelativeLayout>

, , , .

!

+4
1

, , - .

, , setOnClickListener() CustomEditText android:nextFocusDown="" xml.

:

myxml.xml

<com.view.MyCustomEditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:hint="@string/email_username"
    android:inputType="textEmailAddress"
    android:nextFocusDown="@+id/password"/>

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    android:imeOptions="actionDone"/>

MainActivity.java:

mUserEditText = (EditText) findViewById(R.id.username);
mPasswordEditText = (EditText) findViewById(R.id.password);
// Instead of using this
mEditText.setOnClickListener(this)
// Use
mEditText.setOnTouchListener(this)
+1

Source: https://habr.com/ru/post/1525814/


All Articles