和你一起終身學習,這裡是程序員Android
本篇文章主要介紹 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常用來獲取用戶輸入內容,因為我們要規避用戶輸入的內容為空的情況。
實現效果如下:

EditText 輸入保存的字符串不能為空
實現代碼如下:
public class EditTextMethod extends Activity { EditText mEditText; Button mBtn; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_edittext); mEditText = (EditText) findViewById(R.id.test_et); mBtn = (Button) findViewById(R.id.btn_commit); mBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (!TextUtils.isEmpty(mEditText.getText().toString().trim())) { Toast.makeText( EditTextMethod.this, "你輸入的字符串是:" + mEditText.getText().toString().trim(), 1).show(); } else { Toast.makeText(EditTextMethod.this, "輸入的字符串不能為空", 1).show(); } } }); } }
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/232891.html