【问题标题】:Android custom EditText view and NextFocusDownAndroid 自定义 EditText 视图和 NextFocusDown
【发布时间】:2014-03-05 03:15:11
【问题描述】:

在我的注册活动中,在垂直 LinearLayout 中设置了一些自定义 EditText 视图。从上到下。

因为我需要为每个 EditText 提供一些额外的功能,所以我创建了一个自定义 EditText 类来提供这些额外的功能。 该类依赖于在 attrs.xml 中添加并在类中检索并设置为膨胀的 EditText 的一些自定义属性。

除了我似乎无法解决的一件事之外,一切都很好。 我似乎无法让 NextFocusDown 处理我的自定义 EditText 视图。 使用标准 EditText 视图时,它工作正常。当我切换到我的自定义 EditText 时,它会忽略它。 除此属性外,所有其他属性均正常运行。

我错过了什么?有没有人设法在自定义编辑文本视图中使用该属性?

谢谢!

编辑:

这是自定义 EditText 类的一部分:

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

    }
}

这是我在该类中膨胀的自定义编辑文本 xml:

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

这是我用来获取自定义edittext类中的值的attrs.xml文件:

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

这是我用于注册活动的布局 xml 的一小部分,它的两个视图应该使用 NextFocusDown:

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

我希望这里有足够的信息来了解问题所在。

谢谢!

【问题讨论】:

  • 你是用xml做的吗?
  • 请发布一些 sn-p 或您的代码
  • 嗨!我为自定义 EditText 类、自定义 EditText xml 文件、用于获取数据的属性以及包含我使用的自定义 EditText 视图的实际布局 xml 添加了部分代码。请注意,FirstName 视图使用 NextFocusDown 将焦点发送到 LastName 视图。目前它不起作用...
  • 我也有同样的问题。有一个用于更改字体的自定义 edittext 视图。但是,使用时我无法单击其他自定义编辑文本视图。适用于常规文本视图...
  • 没有人回答这个问题,我也遇到了同样的问题,请补充解决方案。

标签: android xml view android-edittext


【解决方案1】:

旧帖子,但我提交此答案以防其他人遇到同样的问题。

就我而言,我发现在有问题的 CustomEditText 上使用 setOnClickListener() 会导致 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)

【讨论】:

  • 这确实是一个老问题。如果我没记错的话,我最终将 setOnEditorActionListener 附加到视图并检查 actionId 是否等于 EditorInfo.IME_ACTION_NEXT。如果是,那么我在需要转到下一个的视图上调用 requesFocus()。我很难检查您的方法现在是否有效:) 我希望这些答案可以为其他人提供解决方案。
【解决方案2】:

您可以将系统 attrs.xml 文件中的 imeOptions 属性复制到您的自定义视图属性中

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="CustomEditText">
        <attr name="imeOptions">
            <flag name="normal" value="0x00000000" />
            <flag name="actionUnspecified" value="0x00000000" />
            <flag name="actionNone" value="0x00000001" />
            <flag name="actionGo" value="0x00000002" />
            <flag name="actionSearch" value="0x00000003" />
            <flag name="actionSend" value="0x00000004" />
            <flag name="actionNext" value="0x00000005" />
            <flag name="actionDone" value="0x00000006" />
            <flag name="actionPrevious" value="0x00000007" />
            <flag name="flagNoPersonalizedLearning" value="0x1000000" />
            <flag name="flagNoFullscreen" value="0x2000000" />
            <flag name="flagNavigatePrevious" value="0x4000000" />
            <flag name="flagNavigateNext" value="0x8000000" />
            <flag name="flagNoExtractUi" value="0x10000000" />
            <flag name="flagNoAccessoryAction" value="0x20000000" />
            <flag name="flagNoEnterAction" value="0x40000000" />
            <flag name="flagForceAscii" value="0x80000000" />
        </attr>
    </declare-styleable>
</resources>

然后,当您使用自定义 EditText 时,将 imeOptions 属性与您的“自定义”(或任何您喜欢的名称)命名空间一起使用

<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"
            custom:imeOptions="actionNext"
            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"
            custom:imeOptions="actionDone"
            android:hint="@string/last"
            android:inputType="textPersonName"
            android:visibility="visible" />
    </RelativeLayout>

最后一步是在您的自定义 EditText 中解析此属性并将其设置为系统属性。

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
int imeOptions = array.getInt(R.styleable.CustomEditText_imeOptions, EditorInfo.IME_NULL);
systemEditText.setImeOptions(imeOptions);
array.recycle();

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多