我目前正在使用一个自定义的EditText看起来像下面的图像,想法是总是开始写在左边,如果是空的,并按照你的方式从一个EditText跳到下一个,但我&#39 ;当我尝试回到上一个框时遇到问题,如果当前框是空的,那么android不会注册OnKeyListener事件,所以我不知道Backspace是否被按下了。
我尝试了很多像这样的https://github.com/GoodieBag/Pinview成功的PinView库,但他们没有提供字体支持,所以我不能像图像一样使用书法的自定义字体。
对此的任何帮助都将受到赞赏,也有任何关于如何整理这些混乱编码的想法。
当前代码:
private void setTextBoxes() {
// Check status of EditText on focus
text_2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_1)) {
text_1.requestFocus();
} else if (hasFocus && !isEditTextEmpty(text_2)) {
text_3.requestFocus();
}
}
});
text_3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_2)) {
text_2.requestFocus();
} else if (hasFocus && !isEditTextEmpty(text_3)) {
text_4.requestFocus();
}
}
});
text_4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_3)) {
text_3.requestFocus();
} else if (hasFocus && !isEditTextEmpty(text_4)) {
text_5.requestFocus();
}
}
});
text_5.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_4)) {
text_4.requestFocus();
} else if (hasFocus && !isEditTextEmpty(text_5)) {
text_6.requestFocus();
}
}
});
text_6.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_5)) {
text_5.requestFocus();
} else if (hasFocus && !isEditTextEmpty(text_6)) {
text_7.requestFocus();
}
}
});
text_7.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_6)) {
text_6.requestFocus();
} else if (hasFocus && !isEditTextEmpty(text_7)) {
text_8.requestFocus();
}
}
});
text_8.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus && isEditTextEmpty(text_7)) {
text_7.requestFocus();
}
}
});
// Check for backspaces
text_2.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, String.format("Backspace pressed %s", keyCode));
if(keyCode == KeyEvent.KEYCODE_DEL) {
text_1.setText("");
text_1.requestFocus();
}
return false;
}
});
text_3.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL) {
text_2.setText("");
text_2.requestFocus();
}
return false;
}
});
text_4.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL) {
text_3.setText("");
text_3.requestFocus();
}
return false;
}
});
text_5.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL) {
text_4.setText("");
text_4.requestFocus();
}
return false;
}
});
text_6.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
text_5.setText("");
text_5.requestFocus();
}
return false;
}
});
text_7.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
text_6.setText("");
text_6.requestFocus();
}
return false;
}
});
text_8.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
text_7.setText("");
text_7.requestFocus();
}
return false;
}
});
// Go to next EditText
text_1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.toString().length() > 0) {
text_2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
text_2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_3.requestFocus();
} else if (s.toString().length() == 0) {
text_1.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
text_3.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_4.requestFocus();
} else if (s.toString().length() == 0) {
text_2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
text_4.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_5.requestFocus();
} else if (s.toString().length() == 0) {
text_3.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
text_5.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_6.requestFocus();
} else if (s.toString().length() == 0) {
text_4.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
text_6.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_7.requestFocus();
} else if (s.toString().length() == 0) {
text_5.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
text_7.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_8.requestFocus();
} else if (s.toString().length() == 0) {
text_6.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
text_8.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
text_8.requestFocus();
} else if (s.toString().length() == 0) {
text_7.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
}
XML(对字体使用书法,这工作正常)
<LinearLayout
android:id="@+id/lyt_pin"
android:layout_width="match_parent"
android:layout_height="46sp"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:orientation="horizontal">
<EditText
android:id="@+id/text_1"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
<EditText
android:id="@+id/text_2"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
<EditText
android:id="@+id/text_3"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
<EditText
android:id="@+id/text_4"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
<EditText
android:id="@+id/text_5"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
<EditText
android:id="@+id/text_6"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:textSize="32sp" />
<EditText
android:id="@+id/text_7"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
<EditText
android:id="@+id/text_8"
fontPath="fonts/RobotoMono-Bold.ttf"
android:layout_width="36sp"
android:layout_height="46sp"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/bg_pin_box"
android:cursorVisible="false"
android:gravity="center"
android:hint="0"
android:inputType="number"
android:maxLength="1"
android:textSize="32sp" />
</LinearLayout>
答案 0 :(得分:0)
对于将来与这个人斗争的人,我找到了一种方法来保留库,然后在ViewGroup中找到每个EditText并以编程方式从那里设置属性
private void traverseEditTexts(ViewGroup v) {
for (int i = 0; i < v.getChildCount(); i++) {
Object child = v.getChildAt(i);
if (child instanceof EditText) {
EditText e = (EditText) child;
e.setTypeface(TypefaceUtils.load(context.getAssets(), "fonts/RobotoMono-Bold.ttf"));
e.setTextColor(getResources().getColor(R.color.pin_text_color));
e.setPadding(8,4,8,4);
e.setTextSize(32);
}
}
}