android自定義控件(郵箱邊寫邊出格式郵箱)

android可以自定義控件,最近接觸到兩種自定義控件,記下,為以後遇到時可以復用

public class AutoCompleteEmailEdit extends AutoCompleteTextView {
    private ArrayList<String> candidateString = null;
    private ArrayAdapter<String> adapter = null;
    private Context mContext = null;
    private String previousUserName = null;
   
   
 public AutoCompleteEmailEdit(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }
 
  public AutoCompleteEmailEdit(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
     }
   public void init() {
         setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);  
         candidateString = new ArrayList<String>();
         adapter = new ArrayAdapter<String>(mContext, R.layout.list_candidate, candidateString);
         setAdapter(adapter);
         setThreshold(1);
     }
  
   public void createCandidateEmail(String name) {
         CharSequence[] mEmailSuffix = null;       
         mEmailSuffix = getResources().getTextArray(R.array.EmailSuffix);

         if (name == null || name.indexOf('@') != -1) {
             return;
         }

         for (int i = 0; i < mEmailSuffix.length; i++) {
             String tempStr = name.concat(mEmailSuffix[i].toString());
             adapter.add(tempStr);

             if (previousUserName != null) {
                 String tempPrevStr = previousUserName.concat(mEmailSuffix[i].toString());

                 adapter.remove(tempPrevStr);
             }
         }

         previousUserName = name;
     }
    
}

 

摘自 chengxibeauty

發佈留言