我希望我的EditText
为数字值,而不是以0
开头,例如:
123
,则确定0123
错误。 这就是我所做的:
if(editText.getText().toString().charAt(0) == '0' ) {
Toast.makeText(getApplicationContext(), "Sorry firs charset must be not 0", Toast.LENGTH_SHORT).show();
}
但只有当我点击按钮(setOnClickListener
)时它才有效。
如何在点击按钮之前检查此内容?
答案 0 :(得分:3)
您可以在yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if( (s.length()> 0) && (s.toString().charAt(0) == '0') ) {
Toast.makeText(getApplicationContext(), "Sorry first charset must be not 0", Toast.LENGTH_SHORT).show();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
{{1}}
答案 1 :(得分:2)
youredittext.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable mEdit)
{
text = mEdit.toString();
if(text.charAt(0) == '0' ) {
Toast.makeText(getApplicationContext(), "Sorry firs charset must be not 0", Toast.LENGTH_SHORT).show();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
答案 2 :(得分:2)
请试试这个:
editText.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.length()>0 && s.charAt(0) == '0') {
Toast.makeText(getApplicationContext(), "Sorry firs charset must be not 0", Toast.LENGTH_SHORT).show();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 3 :(得分:1)
您需要添加TextChangedListener
。
TextChangedListener
向TextWatcher
添加EditText
,并在每次EditText's
文字发生变化时致电。
您希望您的edittext不以“0”开头。所以将TextChangedListener
分配给EditText
,然后检查键入字符的事件。
editText.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable mEdit) {
if(mEdit.toString().length() > 0&& mEdit.toString().charAt(0) == '0' ) {
Toast.makeText(getApplicationContext(), "First letter must not be 0", Toast.LENGTH_SHORT).show();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
答案 4 :(得分:1)
使用android.text.TextWatcher
实现您的活动editText.addTextChangedListener(this);
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(editText.getText().toString().charAt(0) == '0' ) {
Toast.makeText(getApplicationContext(), "Sorry firs charset must be not 0", Toast.LENGTH_SHORT).show();
}
}
}
答案 5 :(得分:0)
onTextChanged 方法用于立即对文本执行更改。它有三个参数。第一个参数是 Charequence s,它是在文本字段中输入的字符串 。接下来的三个int参数适用于Start, Before, Count
。
Your_EditText.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable mEdit)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
if (s.length()>0 && s.charAt(0) == '0')
{
Toast.makeText(getApplicationContext(), "Sorry First Charset must be not 0", Toast.LENGTH_SHORT).show();
}
});