现在的位置: 首页 > 综合 > 正文

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

2017年12月19日 ⁄ 综合 ⁄ 共 1515字 ⁄ 字号 评论关闭
  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.  

 

抱歉!评论已关闭.