我在Android新希望,當我進入的EditText CNIC以再經過一些數字我可以把破折號它想:添加X位後短線在EDITTEXT
12221-1338888-3
et_cnic = (EditText) findViewById(R.id.et_cnic);
我該如何做到這一點?請幫助。
我在Android新希望,當我進入的EditText CNIC以再經過一些數字我可以把破折號它想:添加X位後短線在EDITTEXT
12221-1338888-3
et_cnic = (EditText) findViewById(R.id.et_cnic);
我該如何做到這一點?請幫助。
String content="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edText = (EditText)findViewById(R.id.edText);
edText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(s.length()%4 == 0){
int starting = content.length()==0?0:content.length();
content = content+s.subSequence(starting, start)+"-";
}
}
@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
}
});
}
試試這個
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = s.toString();
if(s.length() == 5 || s.length() == 13){
str += "-";
edt.setText(str);
edt.setSelection(str.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et_cnic .addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(String.valueOf(et_cnic.getText()).length>X)//here x is the number of character after which "." will display on edit text
{
et_cnic.setText(String.valueOf(et_cnic.getText).substring(0,X)+"*")//x here same the number of charcter which you do not want to replace with"."
}
// TODO Auto-generated method stub
// Here you have to write your code for adding dashes.
}
@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
}
});
試試這個:
int x = 2;
edt = (EditText) findViewById(R.id.edt_test);
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() == x) {
edt.setText(s + "-");
edt.setSelection(s.toString().length() + 1);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
我怎麼可以添加破折號? – newOnAndroid
看到我編輯的代碼 – Akash