本篇文章主要介紹 Android 開發中的部分知識點,通過閱讀本篇文章,您將收獲以下內容:
一、EditText 繼承關系
二、EditText 常用舉例
三、EditText 自定義背景框
四、EditText自動檢測輸入內容
五、Edittext 密文顯示
六、EditText 限制只能輸入特定字符
七、EditText 輸入保存的字符串不能為空
一、EditText 繼承關系
EditText繼承關系 如下:
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.EditText
二、EditText 常用舉例
EditText主要用於輸入和修改文本內容。
限制只能輸入純文本內容舉例如下:
<EditText
android:id="@+id/plain_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
三、EditText 自定義背景框
- xml 中使用EditText 控件
<!-- 自定義EditText 背景 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/custom_edittext_background"
android:gravity="center"
android:hint="一、自定義EditText背景框"
android:padding="8dp"
android:textSize="16sp" />
- 自定義 EditText 背景框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 圓角-->
<corners android:radius="5dp" />
<!--描邊-->
<stroke
android:width="1dp"
android:color="@android:color/holo_blue_light" />
</shape>
- 實現效果

四、EditText自動檢測輸入內容
- xml 中使用EditText 控件
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="true"
android:hint="二、自動檢測輸入更正屬性 autoText"
android:textColor="#FF6100" />
- 實現效果

五、Edittext 密文顯示
- xml 中使用EditText 控件
<!-- 以密文的形式顯示 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="三、以密文的形式顯示密碼"
android:password="true" />
- 實現效果

六、EditText 限制只能輸入特定字符
限定只能輸入阿拉伯數字實現如下:
- xml 中使用EditText 控件
<!-- 設置允許輸入的字符 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="123456789.+-*/\n()"
android:hint="四、設置限制允許輸入阿拉伯數字" />
- 實現效果

七、EditText 輸入保存的字符串不能為空
EditText常用來獲取用戶輸入內容,因為我們要規避用戶輸入的內容為空的情況。
實現效果如下:

實現代碼如下:
