Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12393508
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: Android平台

2014-12-09 09:09:49

Android工具箱中的TextFields中,EditText的inputType定义的N种类型,包括了常用的text,number..

image image

下面代码添加一个number类型的EditText并设置它的数字输入范围示范。

image


image

 

设置输入数字范围的代码:

image

如果使用屏蔽的代码,就会出现下面问题:

Cannot refer to a non-final variable et inside an inner class defined in a different method

原因见文章:

主要原因是:

在方法中定义的变量时局部变量,当方法返回时,局部变量(str1,str2)对应的栈就被回收了,当方法内部类去访问局部变量时就会发生错误。当在变量前加上final时,变量就不在是真的变量了,成了常量,这样在编译器进行编译时(即编译阶段)就会用变量的值来代替变量,这样就不会出现变量清除后,再访问变量的错误。

 

下面贴出限定的全部代码:

image

    

  1. private int MIN_MARK = 0;
  2.     private int MAX_MARK = 100;
  3.     //private void setRegion(EditText et)
  4.     private void setRegion( final EditText et)
  5.     {
  6.         et.addTextChangedListener(new TextWatcher() {
  7.             @Override
  8.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  9.                 if (start > 1)
  10.                 {
  11.                     if (MIN_MARK != -1 && MAX_MARK != -1)
  12.                     {
  13.                       int num = Integer.parseInt(s.toString());
  14.                       if (num > MAX_MARK)
  15.                       {
  16.                           s = String.valueOf(MAX_MARK);
  17.                           et.setText(s);
  18.                       }
  19.                       else if(num < MIN_MARK)
  20.                           s = String.valueOf(MIN_MARK);
  21.                       return;
  22.                     }
  23.                 }
  24.             }

  25.             @Override
  26.             public void beforeTextChanged(CharSequence s, int start, int count,
  27.                     int after) {
  28.             }

  29.             @Override
  30.             public void afterTextChanged(Editable s)
  31.             {
  32.                 if (s != null && !s.equals(""))
  33.                 {
  34.                     if (MIN_MARK != -1 && MAX_MARK != -1)
  35.                     {
  36.                          int markVal = 0;
  37.                          try
  38.                          {
  39.                              markVal = Integer.parseInt(s.toString());
  40.                          }
  41.                          catch (NumberFormatException e)
  42.                          {
  43.                              markVal = 0;
  44.                          }
  45.                          if (markVal > MAX_MARK)
  46.                          {
  47.                              Toast.makeText(getBaseContext(), "分数不能超过100", Toast.LENGTH_SHORT).show();
  48.                              et.setText(String.valueOf(MAX_MARK));
  49.                          }
  50.                          return;
  51.                     }
  52.                  }
  53.             }
  54.         });
  55.     }

image

参考文献:

http://blog.csdn.net/dahuaishu2010_/article/details/8296687

阅读(30538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~