我想自动放一个" - "在我的EditText中输入6个字母后,我希望用户可以在" - "之后继续写。 (我希望用户写道:1234562,它在EditText中显示为123456-2。)
但我不知道如何做到这一点,如果有可能的话。如果你能帮助我,谢谢
答案 0 :(得分:11)
添加textwatcher。然后使用以下代码:
@Override
public void afterTextChanged(Editable text) {
if (text.length() == 6) {
text.append('-');
}
}
您还可以在if语句中使用多个条件,如:
if (text.length() == 3 || text.length() == 6) {
text.append('-');
}
答案 1 :(得分:2)
EditText editText = (EditText) findViewById(R.id.search);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable text) {
// TODO Auto-generated method stub
if (text.length() == 6) {
text.append('-');
}
}
});
答案 2 :(得分:2)
EditText editText = (EditText) findViewById(R.id.editText1);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String text = editText.getText().toString();
if(text.length() == 6){
editText.append("-");
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
答案 3 :(得分:1)
you can also delete characters with this:
text.addTextChangedListener(new TextWatcher() {
int first = 0;
int second;
@Override
public void afterTextChanged(Editable s) {
second = first;
first = s.length();
if(text.length()==6 && first>second){
text.append("-");
}
}
答案 4 :(得分:0)
我使用发现的here代码来回答这个问题,它对我来说非常有效,我真的希望这可以对其他人有所帮助。这是针对此特定问题修改的代码:
def to_representation(self, instance):
data = super(UserSerializer, self).to_representation(instance)
userprofile = data.pop('userprofile')
for key, val in userprofile.items():
data.update({key: val})
return data
答案 5 :(得分:0)
这是我对 Kotlin 的看法:
type DimensionMemberIntersection string
type Account struct {
ID string
Name string
Reference string
MembersIntersection []DimensionMemberIntersection
// This assumes the underlying type of DimensionMemberIntersection is string
}
account := Account{}
for _, col := range account.MembersIntersection {
if !db.Migrator().HasColumn(&Account{}, col) {
db.Migrator().AddColumn(&Account{}, col)
}
}