Code example for EditText

Methods: getTextlengthsetText, requestFocus

0
	private static final int MAX_TWEET_LENGTH = 140;
	private static final int MAX_TWEET_INPUT_LENGTH = 400;
 
	public void setTextAndFocus(String text, boolean start) {
		setText(text);
		Editable editable = mEditText.getText();
		if (!start) {
			Selection.setSelection(editable, editable.length());
		} else { 
			Selection.setSelection(editable, 0);
		} 
		mEditText.requestFocus();
	} 
 
	public void setText(String text) {
		mEditText.setText(text);
		updateCharsRemain(); 
	} 
 
	private TextWatcher mTextWatcher = new TextWatcher() {
		@Override 
		public void afterTextChanged(Editable e) { 
			updateCharsRemain(); 
		} 
 
		@Override 
		public void beforeTextChanged(CharSequence s, int start, int count, 
				int after) { 
		} 
 
		@Override 
		public void onTextChanged(CharSequence s, int start, int before, 
				int count) { 
		} 
	}; 
 
	public void updateCharsRemain() { 
		int remaining = MAX_TWEET_LENGTH - mEditText.length();
		if (remaining < 0) {
			mCharsRemainText.setTextColor(Color.RED);
		} else { 
			mCharsRemainText.setTextColor(originTextColor);
		}