android源代码提示文本框还能输入多少个字符

 
 
  1. public class TestAndroidActivity extends Activity { 
  2.     /** Called when the activity is first created. */ 
  3.      
  4.      Button clearbtn; 
  5.      EditText et; 
  6.      TextView tv; 
  7.      final int MAX_LENGTH = 50
  8.      int Rest_Length = MAX_LENGTH; 
  9.      
  10.     @Override 
  11.     public void onCreate(Bundle savedInstanceState) { 
  12.         super.onCreate(savedInstanceState); 
  13.         setContentView(R.layout.main); 
  14.          
  15.         //tv =(TextView)findViewById(R.); 
  16.     tv=(TextView) this.findViewById(R.id.btn); 
  17.         //tv=this.findViewById(R.) 
  18.         et = (EditText) this.findViewById(R.id.bg); 
  19.  
  20.         et.addTextChangedListener(new TextWatcher() { 
  21.              
  22.             public void onTextChanged(CharSequence s, int start, int before, int count) { 
  23.                 if(Rest_Length>0){ 
  24.                     Rest_Length = MAX_LENGTH - et.getText().length(); 
  25.                 } 
  26.             } 
  27.              
  28.             public void beforeTextChanged(CharSequence s, int start, int count, 
  29.                     int after) { 
  30.                 tv.setText("还可以输入"+Rest_Length+"个字"); 
  31.                  
  32.             } 
  33.              
  34.             public void afterTextChanged(Editable s) { 
  35.                 tv.setText("还可以输入"+Rest_Length+"个字"); 
  36.             } 
  37.         }); 
  38.  
  39.         clearbtn = (Button)findViewById(R.id.btn); 
  40.         clearbtn.setOnClickListener(new Button.OnClickListener() { 
  41.              
  42.             public void onClick(View v) { 
  43.                 et.setText(""); 
  44.                 Rest_Length = MAX_LENGTH; 
  45.             } 
  46.         }); 
  47.     } 
  48.  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值