1、给EditText加上文字选中功能,比如微博的插入话题功能。点击“插入话题”按钮的时候,“#请插入话题名称#”在两个#号中间的内容处于选中状态,用户一点击即消失。代码如下:

 

  1. Java代码  
  2.       
  3. text.setText("#请插入话题名称#");        
  4. Editable editable = text.getText();        
  5. Selection.setSelection(editable, 1, editable.length() - 1);    

2、如果想默认进入一个Activity时,唯一的一个edittext先不要获得焦点。在EditText前面加上一个没有大小的Layout:

 

  1. XML/HTML代码  
  2.       
  3. <LinearLayout        
  4.     android:focusable="true" android:focusableInTouchMode="true"        
  5.     android:layout_width="0px" android:layout_height="0px"/>    

3、输入文字的时候,如果想限制字数,并提示用户,可用以下方法:

 

  1. Java代码  
  2.       
  3. text.addTextChangedListener(new TextWatcher() {        
  4.                     
  5.             @Override        
  6.             public void onTextChanged(CharSequence s, int start, int before, int count) {        
  7.                  textCount = textCount + count - before;        
  8.                  if (textCount <= 140) {        
  9.                        writeWordDes.setText("可输入字数:" + (140 - textCount));        
  10.                        writeWordDes.setTextColor(getResources().getColor(R.color.solid_black));        
  11.                  } else {        
  12.                      writeWordDes.setText("超出字数:" + (textCount - 140));        
  13.                      writeWordDes.setTextColor(getResources().getColor(R.color.solid_red));        
  14.                  }        
  15.             }        
  16.                     
  17.             @Override        
  18.             public void beforeTextChanged(CharSequence s, int start, int count,        
  19.                     int after) {        
  20.                         
  21.             }        
  22.                     
  23.             @Override        
  24.             public void afterTextChanged(Editable s) {        
  25.                         
  26.             }        
  27.         });        
  28.     }    

4、让EditText不可输入,比如超过一定字数后,不让用户再输入文字:

 

  1. Java代码  
  2.       
  3. text.setFilters(new InputFilter[] {          
  4.          new InputFilter() {          
  5.                 public CharSequence filter(CharSequence source, int start,          
  6.                         int end, Spanned dest, int dstart, int dend) {          
  7.                     return source.length() < 1 ? dest.subSequence(dstart, dend) : "";          
  8.                 }          
  9.             }          
  10.         });