public class
EditText
extends TextView
java.lang.Object
? android.view.View
? android.widget.TextView
? android.widget.EditText
由繼承結(jié)構(gòu)可以看出,EditText繼承自TextView,因此TextView中的一些屬性和方法也可以在EditText中使用。EditText的常用屬性參照下表:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/2-1.png" alt="這里寫圖片描述" />
下面通過一段代碼來演示一下上面的屬性:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="電話號碼:"
android:textSize="18sp" />
<EditText
android:id="@+id/et_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_num"
android:hint="這里是提示內(nèi)容,只能輸入整數(shù)"
android:numeric="integer"
android:selectAllOnFocus="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/et_num"
android:layout_marginTop="41dp"
android:padding="5dp"
android:text="密碼:"
android:textSize="18sp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:drawableRight="@android:drawable/ic_lock_lock"
android:hint="密文顯示輸入,最長8位"
android:maxLength="8"
android:password="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="51dp"
android:padding="5dp"
android:text="不可編輯:"
android:textSize="18sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/et_num"
android:layout_alignStart="@+id/et_num"
android:editable="false"
android:hint="這里是提示內(nèi)容,內(nèi)容不可編輯" />
</RelativeLayout>
顯示效果如下:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/2-2.png" alt="這里寫圖片描述" />
第一個輸入框只能輸入數(shù)字,第二個輸入框輸入按密文顯示,且最長只能輸入8位,第三個輸入框不可編輯。
EditText的常用方法參照下表:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/2-3.png" alt="這里寫圖片描述" />
測試用例:
public class MainActivity extends AppCompatActivity {
EditText editTextNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextNum=(EditText)findViewById(R.id.et_num);
editTextNum.setText("setText方法設(shè)置");//設(shè)置EditText文字
editTextNum.setSelection(1); //設(shè)置光標(biāo)起始位置
editTextNum.selectAll(); //全選
editTextNum.setEnabled(false); //不可編輯
}
}
運行實例:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/2-4.png" alt="這里寫圖片描述" />
主要將表格中的方法應(yīng)用到第一個EditText中,設(shè)置了初始顯示文字和初始光標(biāo)位置,并全選了全部內(nèi)容,最后將EditText設(shè)置成不可編輯。 實戰(zhàn)演練:
通過自定義View的方式實現(xiàn)一個提供一鍵刪除全部內(nèi)容的控件,布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="登 錄"
android:textSize="25sp" />
<demo.androidwar.com.edittextdemo.DeleteEditText
android:id="@+id/det_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:drawableLeft="@drawable/user_account"
android:drawableRight="@drawable/user_delete"
android:ems="10"
android:hint="請輸入帳號名" />
<demo.androidwar.com.edittextdemo.DeleteEditText
android:id="@+id/user_password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/det_test"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/user_password"
android:drawableRight="@drawable/user_delete"
android:ems="10"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:singleLine="true" />
</RelativeLayout>
引入了自定義控件DeleteEditText,設(shè)置了drawableLeft和drawableRight屬性,在控件的左右兩側(cè)分別設(shè)置了圖片。
public class DeleteEditText extends EditText {
private Drawable mRightDrawable;
boolean isHasFocus;
//構(gòu)造方法1
public DeleteEditText(Context context) {
super(context);
init();
}
//構(gòu)造方法2
public DeleteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
//構(gòu)造方法3
public DeleteEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
Drawable drawables[] = this.getCompoundDrawables();//本方法獲取控件上下左右四個方位插入的圖片
mRightDrawable = drawables[2];
this.addTextChangedListener(new TextWatcherImpl());
this.setOnFocusChangeListener(new OnFocusChangeImpl());
setClearDrawableVisible(false);//初始設(shè)置所有右邊圖片不可見
}
private class OnFocusChangeImpl implements OnFocusChangeListener{
@Override
public void onFocusChange(View v, boolean hasFocus) {
isHasFocus = hasFocus;
if (isHasFocus) {//如果獲取焦點
boolean isNoNull = getText().toString().length() >= 1;
setClearDrawableVisible(isNoNull);
}else{
setClearDrawableVisible(false);
}
}
}
//本方法控制右邊圖片的顯示與否
private void setClearDrawableVisible(boolean isNoNull) {
Drawable rightDrawable;
if (isNoNull) {
rightDrawable = mRightDrawable;
} else {
rightDrawable = null;
}
// 使用代碼設(shè)置該控件left, top, right, and bottom處的圖標(biāo)
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], rightDrawable,
getCompoundDrawables()[3]);
}
private class TextWatcherImpl implements TextWatcher {
//下面是三個要覆寫的方法
@Override
public void afterTextChanged(Editable s) {//內(nèi)容輸入后
boolean isNoNull = getText().toString().length() >= 1;
setClearDrawableVisible(isNoNull);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//內(nèi)容輸入前
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//內(nèi)容輸入中
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
int length1 = getWidth() - getPaddingRight();//刪除圖片右側(cè)到EditText控件最左側(cè)距離
int length2 = getWidth() - getTotalPaddingRight();//刪除圖片左側(cè)到EditText控件最左側(cè)距離
boolean isClean = (event.getX() > length2)
&& (event.getX() < length1);
if (isClean) {
setText("");
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
這里需要說明一下幾點:
getCompoundDrawables 方法:其官方解釋是Returns drawables for the left, top, right, and bottom borders,翻譯過來也就是說返回控件上下左右配置的四張圖片,并轉(zhuǎn)換成了drawable對象,看一下配置文件的引入了左右兩張圖片,也就是left和top可以返回drawable對象,對應(yīng)數(shù)組的0和2下標(biāo),而top和bottom則返回空。這里只對右側(cè)圖片進行操作,因此將drawable[2]傳遞給mRightDrawable,方便后面的方法操作引用。
給EditText增加了監(jiān)聽器:addTextChangedListener,當(dāng)EditText內(nèi)容發(fā)生改變時觸發(fā),這時使用getText方法獲取EditText中內(nèi)容,并判斷其長度是否大于1,大于1則說明輸入內(nèi)容不為空,這時設(shè)置最右邊的圖片(也就是清空圖片)顯示。
setClearDrawableVisible是我們自定義的方法,根據(jù)傳入?yún)?shù)控制清空圖片的顯示與否,這里調(diào)用了setCompoundDrawables方法,當(dāng)決定顯示圖片時將mRightDrawable當(dāng)做參數(shù)進行傳遞,否則傳遞Null,即隱藏清空圖片。
除了設(shè)置了內(nèi)容變化監(jiān)聽,還設(shè)置了焦點變化監(jiān)聽,控件獲得焦點時,調(diào)用getText方法獲取EditText中的內(nèi)容,判斷其長度決定清空圖片的顯示與否,失去焦點時隱藏清空圖片。
運行實例,如下圖: http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/2-6.png" alt="這里寫圖片描述" />