作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了一个用于创建密码布局的库项目。我创建了四个用于输入密码的文本框,每个文本框只能包含一个字符。但是我无法创建用于返回密码的事件。我想在用户输入 4 位密码时触发一个事件。
这是 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_passcodedigits"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<EditText
android:id="@+id/et_passcode"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:maxLength="1" />
<EditText
android:id="@+id/et_passcode2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:maxLength="1" />
<EditText
android:id="@+id/et_passcode3"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:maxLength="1" />
<EditText
android:id="@+id/et_passcode4"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:maxLength="1" />
</LinearLayout>
</LinearLayout>
这是类的代码:
public class Passcode extends LinearLayout {
EditText firstEditText, secondEditText, thirdEditText, fourEditText;
public Passcode(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Passcode(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.passcode_layout, this);
loadViews();
}
private void loadViews() {
firstEditText = (EditText) findViewById(R.id.et_passcode);
secondEditText = (EditText) findViewById(R.id.et_passcode2);
thirdEditText = (EditText) findViewById(R.id.et_passcode3);
fourEditText = (EditText) findViewById(R.id.et_passcode4);
// Text changed event for first EditText
firstEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.length() == 1) {
firstEditText.setFocusable(false);
secondEditText.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
// Text changed event for second EditText
secondEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.length() == 1) {
secondEditText.setFocusable(false);
thirdEditText.requestFocus();
} else if (s.length() == 0) {
firstEditText.setFocusable(true);
firstEditText.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
// Text changed event for third EditText
thirdEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.length() == 1) {
thirdEditText.setFocusable(false);
fourEditText.requestFocus();
} else if (s.length() == 0) {
secondEditText.setFocusable(true);
secondEditText.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
// Text changed event for fourth EditText
fourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.length() == 1) {
thirdEditText.setFocusable(false);
} else if (s.length() == 0) {
thirdEditText.setFocusable(true);
thirdEditText.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
}
最佳答案
您可以定义自己的监听器。
将此添加到密码
:
public interface OnPasscodeCompletedListener {
public void onComplete(String passcode);
}
private OnPasscodeCompletedListener listener = null;
public void setOnPasscodeCompletedListener(OnPasscodeCompletedListener listener) {
this.listener = listener;
}
然后,在所有更改文本的监听器中,当您拥有所有 4 位数字时添加此内容:
if (listener != null) {
// Concatenate the values from EditText to create the passcode
listener.onComplete(passcode);
}
之后,像使用 onClickListener
一样使用它。例如:
private OnPasscodeCompletedListener onPasscodeCompletedListener
= new OnPasscodeCompletedListener() {
@Override
public void onComplete(String passcode) {
Log.i(TAG, "passcode = " + passcode);
}
};
passcodeView.setOnPasscodeCompletedListener(onPasscodeCompletedListener);
关于android - 如何为自定义控件创建事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22914072/
我是一名优秀的程序员,十分优秀!