1. 程式人生 > 實用技巧 >Android開發筆記(五)——EditText

Android開發筆記(五)——EditText

EditText也是TextView的一個子類,TextView和Button的一些屬性在EditText中也同樣適用。

TextView是顯示文字的一個控制元件,但是不可以編輯,EditText是可以輸入的控制元件。

activity_main.xml 中新增一個Button:

<Button
    android:id="@+id/btn_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="EditText"
    android:textAllCaps="false"
    />

Button中的英文字母會自動進行大寫轉換, android:textAllCaps="false" 可以取消這個預設。
接下來在 MainActivity.java 中宣告這個控制元件:

private Button mBtnEditText;

新增一個新的activity名稱叫 EditTextActivity ,此時 AndroidMainfest.xml 中會自動新增如下宣告:

<activity android:name=".EditTextActivity"></activity>

如果沒有新增則要自己手動新增,否則會跳轉失敗。
之後要在 MainActivity.java

中的 onCreate 函式中使用 findViewById 找到該button,接著設定一個點選事件,使其點選之後跳轉到新的介面:

mBtnEditText=findViewById(R.id.btn_edittext);
mBtnEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //跳轉到EditText的演示介面
        Intent intent=new Intent(MainActivity.this,EditTextActivity.class);
        startActivity(intent);
    }
});

常用屬性

hint => 文字框內提示
textColorHint => 設定hint提示文字顏色
inputType =>
  "none"//輸入普通字元
  "text"//輸入普通字元
  "textCapCharacters"//輸入普通字元
  "textCapWords"//單詞首字母大小
  textCapSentences"//僅第一個字母大小
  "textAutoCorrect"//前兩個自動完成
  "textAutoComplete"//前兩個自動完成
  "textMultiLine"//多行輸入
  "textImeMultiLine"//輸入法多行(不一定支援)
  "textNoSuggestions"//不提示
  "textUri"//URI格式
  "textEmailAddress"//電子郵件地址格式
  "textEmailSubject"//郵件主題格式
  "textShortMessage"//短訊息格式
  "textLongMessage"//長訊息格式
  "textPersonName"//人名格式
  "textPostalAddress"//郵政格式
  "textPassword"//密碼格式
  "textVisiblePassword"//密碼可見格式
  "textWebEditText"//作為網頁表單的文字格式
  "textFilter"//文字篩選格式
  "textPhonetic"//拼音輸入格式
  "number"//數字格式
  "numberSigned"//有符號數字格式
  "numberDecimal"//可以帶小數點的浮點格式
  "phone"//撥號鍵盤
  "datetime"//日期+時間格式
  "date"//日期鍵盤
  "time"//時間鍵盤
drawableLeft => 編輯框內左側繪製圖片資源,= Start | 也有Top Bottom Right
drawablePadding => 圖片padding
lines => 佔幾行(顯示上)
digits => 設定只接收指定的文字內容
textAlignment => center(居中),inherit(預設,居左邊顯示),viewStart(居左顯示),viewEnd(居右顯示),textStart(居左顯示),textEnd(居右顯示).
extCursorDrawable => 游標顏色
android:textScaleX => 文字水平縮放係數.
android:typeface => hint字型
maxLength => 最多接收文字長度
maxHeight => 文字區域最大高度
minHeight => 文字區域最小高度
scrollHorizontally => 文字超出,是否出現橫拉條
ellipsize => 文字過長時,如何顯示
  "start”開頭省略
  ”end”結尾省略
  ”middle”中間省略
  ”marquee” 跑馬燈

簡單登入介面的製作

activity_edit_text.xml 中新增 EditTextButton 控制元件,具體屬性設定如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    >

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="20sp"
        android:textColor="#000000"
        android:hint="手機號"
        android:inputType="number"
        android:background="@drawable/bg_username"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:drawableLeft="@drawable/icon_user"
        android:drawablePadding="10dp"
        android:maxLines="1"
        android:layout_marginTop="50dp"
        />

    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="20sp"
        android:textColor="#000000"
        android:hint="密碼"
        android:inputType="textPassword"
        android:layout_below="@id/et_1"
        android:layout_marginTop="15dp"
        android:background="@drawable/bg_username"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:drawableLeft="@drawable/icon_password"
        android:drawablePadding="10dp"
        android:maxLines="1"
        />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/et_2"
        android:layout_marginTop="40dp"
        android:text="登入"
        android:textColor="#000000"
        android:textSize="20sp"
        android:background="@drawable/bg_btn4"
        />

</RelativeLayout>

接下來在 EditTextActivity.java 中宣告登入的 Button 控制元件:

private Button mBtnLogin;

之後要在 EditTextActivity.java 中的 onCreate 函式中使用 findViewById 找到該button,接著設定一個點選事件,使其點選之後顯示登入成功的字樣:

mBtnLogin=findViewById(R.id.btn_login);
mBtnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(EditTextActivity.this,"登入成功!",Toast.LENGTH_SHORT).show();
    }
});

具體效果如下:這裡輸入密碼的時候手機錄屏錄不到o(╥﹏╥)o

監聽事件

對輸入內容變化的監聽:
這裡對輸入的手機號進行監聽:
EditTextActivity.java 中宣告輸入框的 EditText 控制元件:

private EditText mEtUsername;

之後要在 EditTextActivity.java 中的 onCreate 函式中使用 findViewById 找到該button,接著設定一個監聽事件,實現可以監聽輸入內容,具體實現如下:

mEtUsername=findViewById(R.id.et_1);
mEtUsername.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) {
        Log.d("edittext",s.toString());
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

CharSequence s 就是當前輸入框的內容。

具體效果如下:

可以看到,每當輸入一個內容就可以打印出來,這就實現的對輸入內容的監聽。