在android中控制edittext内容
我有一些编辑文本来获取一些整数。 现在我想控制它们的内容。e、 如果写的数字在1到22之间,做些什么 如何从editText获取文本并将其转换为int以进行比较?使用 在你的myTextWatcher中在android中控制edittext内容,android,android-edittext,logic,Android,Android Edittext,Logic,我有一些编辑文本来获取一些整数。 现在我想控制它们的内容。e、 如果写的数字在1到22之间,做些什么 如何从editText获取文本并将其转换为int以进行比较?使用 在你的myTextWatcher中 TextWatcher myTextWatcher= new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
TextWatcher myTextWatcher= new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(Integer.parseInt(s)>1 && Integer.parseInt(s)<=22)
{
// perform you action
}
}
public void afterTextChanged(Editable s) {
};
TextWatcher myTextWatcher=newtextWatcher(){
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
public void onTextChanged(字符序列、int start、int before、int count){
如果(Integer.parseInt)>1&&Integer.parseInt请在按钮的onClickListener中尝试以下操作:
try {
int number = Integer.parseInt(editText.getText().toString());
if(number >= 1 && number < 22) {
// do smth
}
} catch (NumberFormatException e){
Log.d(LOG_TAG, "Could not convert text into integer");
}
试试看{
int number=Integer.parseInt(editText.getText().toString());
如果(编号>=1&&number<22){
//做smth
}
}捕获(数字格式){
d(Log_标记,“无法将文本转换为整数”);
}
如果您的按钮是btn
且编辑文本是edt
,则执行以下操作
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int a;
a=Integer.parseInt(edt.getText().toString());
if(a>1 && a<22)
{
//do something here
}
}
});
btn.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
INTA;
a=Integer.parseInt(edt.getText().toString());
如果(a>1&&a什么时候发生?单击按钮后?是的!没错,我忘记写它了,但是为什么写数字!=null,数字是整数而不是布尔值?检查空指针总是好的。:)但是在这种情况下你是对的。除了数字以外的任何字符串都是无效的,并且会引发NumberFormatException。我将编辑我的答案。是。a
是整数值@MinaDahesh
try {
int number = Integer.parseInt(editText.getText().toString());
if(number >= 1 && number < 22) {
// do smth
}
} catch (NumberFormatException e){
Log.d(LOG_TAG, "Could not convert text into integer");
}
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int a;
a=Integer.parseInt(edt.getText().toString());
if(a>1 && a<22)
{
//do something here
}
}
});