1、TextView
Android:lineSpacingExtra="5dp"設置行間距,如”3dp”。
android:lineSpacingMultiplier="1.2"設置行間距的倍數,如”1.2″。
超出制定長度顯示省略號
android:ellipsize="end"
android:maxEms="11"
android:singleLine="true"
2、CheckBox
android:id="@+id/checkbox"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/space_10"
android:background="@drawable/checkbox"
android:button="@null"
android:checked="true" />
3、EditText
personalEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(number)});代碼設置輸入字數限制
android:maxLength="10" ?XML設置輸入字數限制
android:background="@null"無背景
android:inputType="textMultiLine"多行文本輸入
password_edit.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);密碼可見
password_edit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);密碼不可見
android:windowSoftInputMode="adjustPan" ?(AndroidManifest.xml文件中對應的activity里面)彈出軟鍵盤,布局被上頂的問題
在EditText的父級控件中找一個,設置成
android:focusable="true"
android:focusableInTouchMode="true"
更多屬性詳見:http://blog.csdn.net/qyf_5445/article/details/8651740
//監聽EditText輸入
mLoginNameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
帶清除按鈕的EidtText
public classClearEditTextextendsEditTextimplementsView.OnFocusChangeListener,TextWatcher {
/**
*刪除按鈕的引用
*/
privateDrawablemClearDrawable;
private booleanhasFoucs;
publicClearEditText(Context context) {
this(context, null);
}
publicClearEditText(Context context,AttributeSet attrs) {
//這里構造方法也很重要,不加這個很多屬性不能再XML里面定義
this(context,attrs,android.R.attr.editTextStyle);
}
publicClearEditText(Context context,AttributeSet attrs, intdefStyle) {
super(context,attrs,defStyle);
init();
}
private voidinit() {
//獲取EditText的DrawableRight,假如沒有設置我們就使用默認的圖片,2是獲得右邊的圖片? 順序是左上右下(0,1,2,3,)
mClearDrawable= getCompoundDrawables()[2];
if(mClearDrawable==null) {
// throw new
// NullPointerException("You can add drawableRight attribute in XML");
mClearDrawable= getResources().getDrawable(R.mipmap.delete_account);
}
mClearDrawable.setBounds(0,0,mClearDrawable.getIntrinsicWidth(),mClearDrawable.getIntrinsicHeight());
//默認設置隱藏圖標
setClearIconVisible(false);
//設置焦點改變的監聽
setOnFocusChangeListener(this);
//設置輸入框里面內容發生改變的監聽
addTextChangedListener(this);
}
/**
*因為我們不能直接給EditText設置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件 當我們按下的位置 在EditText的寬度-
*圖標到控件右邊的間距-圖標的寬度 和EditText的寬度-圖標到控件右邊的間距之間我們就算點擊了圖標,豎直方向就沒有考慮
*/
@Override
public booleanonTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(getCompoundDrawables()[2] !=null) {
booleantouchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));
if(touchable) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
/**
*當ClearEditText焦點發生變化的時候,判斷里面字符串長度設置清除圖標的顯示與隱藏
*/
@Override
public voidonFocusChange(View v, booleanhasFocus) {
this.hasFoucs= hasFocus;
if(hasFocus) {
setClearIconVisible(getText().length() >0);
}else{
setClearIconVisible(false);
}
}
/**
*設置清除圖標的顯示與隱藏,調用setCompoundDrawables為EditText繪制上去
*
*@paramvisible
*/
protected voidsetClearIconVisible(booleanvisible) {
Drawable right = visible ?mClearDrawable:null;
setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]);
}
/**
*當輸入框里面內容發生變化的時候回調的方法
*/
@Override
public voidonTextChanged(CharSequence s, intstart, intcount, intafter) {
if(hasFoucs) {
setClearIconVisible(s.length() >0);
}
}
@Override
public voidbeforeTextChanged(CharSequence s, intstart, intcount, intafter) {
}
@Override
public voidafterTextChanged(Editable s) {
}
/**
*設置晃動動畫
*/
public voidsetShakeAnimation() {
this.setAnimation(shakeAnimation(5));
}
/**
*晃動動畫
*
*@paramcounts1秒鐘晃動多少下
*@return
*/
public staticAnimationshakeAnimation(intcounts) {
Animation translateAnimation =newTranslateAnimation(0,10,0,0);
//設置一個循環加速器,使用傳入的次數就會出現擺動的效果。
translateAnimation.setInterpolator(newCycleInterpolator(counts));
translateAnimation.setDuration(500);
returntranslateAnimation;
}
}