在我的註冊活動中,有一些自定義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>
我希望在這裏有足夠的信息,看看是什麼問題。
謝謝!
你在XML幹什麼呢? – user2511882
請張貼一些摘要或您的代碼 – theLazyFinder
嗨!我爲自定義EditText類,自定義EditText xml文件,用於獲取數據的屬性以及保存我使用的自定義EditText視圖的實際佈局xml添加了部分代碼。 請注意,FirstName視圖使用NextFocusDown將焦點發送到LastName視圖。 目前它不起作用... –