2014-02-07 151 views
4

在我的註冊活動中,有一些自定義EditText視圖在垂直LinearLayout中設置。從上到下。Android自定義EditText視圖和NextFocusDown

因爲每個EditText都需要一些額外的功能,所以我創建了一個自定義的EditText類來提供額外的功能。 該類依賴於在attrs.xml中添加的一些自定義屬性,並在該類中檢索,然後設置爲膨脹的EditText。

一切工作正常,除了一件事,我似乎無法解決。 我似乎無法讓我的自定義EditText視圖的NextFocusDown工作。 使用標準的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> 

我希望在這裏有足夠的信息,看看是什麼問題。

謝謝!

+0

你在XML幹什麼呢? – user2511882

+0

請張貼一些摘要或您的代碼 – theLazyFinder

+0

嗨!我爲自定義EditText類,自定義EditText xml文件,用於獲取數據的屬性以及保存我使用的自定義EditText視圖的實際佈局xml添加了部分代碼。 請注意,FirstName視圖使用NextFocusDown將焦點發送到LastName視圖。 目前它不起作用... –

回答

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) 
+1

這確實是一個老問題。如果我沒有記錯,我最終將一個setOnEditorActionListener附加到視圖中,並檢查actionId是否等於EditorInfo.IME_ACTION_NEXT。如果是這樣,那麼我需要對下一個視圖調用requesFocus()。 很難讓我檢查你的方法是否正常工作:) 我希望這些答案可以爲他人提供解決方案。 –