android_EditText相關內容

一、限制edittext的最大長度

1、Xml代碼 

android:maxLength =“50”

2、Java代碼 

editText.setFilters( new InputFilter[]{ new InputFilter.LengthFilter( 100 )});


二、限制EditText輸入框的內容

android:digits="1234567890.+-*/%\n()"

限制輸入框中只能輸入自己定義的這些字符串 如果輸入其它將不予以顯示

android:phoneNumber="true"

限制輸入框中只能輸入手機號碼

android:password="true"

限制輸入框中輸入的任何內容將以"*"符號來顯示

android:hint="默認文字"

輸入內容前默認顯示在輸入框中的文字

android:textColorHint="#FF0000"

設置文字內容顏色

android:enabled="false"

設置輸入框不能被編輯


三、編輯框中顯示圖片

<EditText

android:layout_width="wrap_content"  

android:layout_height="wrap_content"

android:text="在圖片下方"

android:textColor="#FF0000"

android:drawableBottom="@drawable/jay"

android:layout_alignParentTop="true"

 android:layout_centerHorizontal="true">  

</EditText>


四、設置軟鍵盤的Enter鍵

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/textviewll"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<EditText android:id="@+id/txtTest0"

android:imeOptions="actionGo"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="特殊按鈕-去往"

></EditText>

<EditText android:id="@+id/txtTest1"

android:imeOptions="actionSearch"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="特殊按鈕-搜索"

></EditText>

<EditText android:id="@+id/txtTest2"

android:imeOptions="actionSend"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="特殊按鈕-發送"

></EditText>

<EditText android:id="@+id/txtTest3"

android:imeOptions="actionNext"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="特殊按鈕-下一個"

></EditText>

<EditText android:id="@+id/txtTest4"

android:imeOptions="actionDone"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="特殊按鈕-完成"

></EditText>

<EditText android:id="@+id/txtTest5"

android:imeOptions="actionUnspecified"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

 android:hint="特殊按鈕-未指定"></EditText>  

</LinearLayout>


五、監聽軟鍵盤的按鍵事件

editText.addTextChangedListener(new TextWatcher() {

@Override  

public void onTextChanged(CharSequence text, int start, int before, int count) {

//text 輸入框中改變後的字符串信息

//start 輸入框中改變後的字符串的起始位置

//before 輸入框中改變前的字符串的位置 默認爲0

//count 輸入框中改變後的一共輸入字符串的數量

textView1.setText("輸入後字符串 [ " + text.toString() + " ] 起始光標 [ " + start + " ] 輸入數量 [ " + count+" ]");

}  

@Override  

public void beforeTextChanged(CharSequence text, int start, int count,int after) {

//text 輸入框中改變前的字符串信息

//start 輸入框中改變前的字符串的起始位置

//count 輸入框中改變前後的字符串改變數量一般爲0

//after 輸入框中改變後的字符串與起始位置的偏移量

System.out.println(text.toString());

textView0.setText("輸入前字符串 [ " + text.toString() + " ]起始光標 [ " + start + " ]結束偏移量 [" + after + " ]");

}

@Override  

public void afterTextChanged(Editable edit) {

//edit 輸入結束呈現在輸入框中的信息

textView2.setText("輸入結束後的內容爲 [" + edit.toString()+" ] 即將顯示在屏幕上");

}

});











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章