1. 程式人生 > >Android小技巧——EditText

Android小技巧——EditText

1、EditText限制最大行數

android:maxLines = "2"  //限制最大的行數為兩行

2、修改游標顏色

android:textCursorDrawable="@null"

@null表示和文字顏色一樣, 特定的顏色可以使用
android:textCursorDrawable=”@color/xxx”

3、EditText游標不顯示

android:cursorVisible="true"
android:textCursorDrawable="@null"

4、修改游標樣式

在drawable下新建檔案text_cursor.xml
內容為

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="@color/gateway_main_title_selected"  />
</shape>

在edittext那邊增加

android:textCursorDrawable="@drawable/text_cursor"

5、設定edittext游標位置

在activity中增加如下程式碼:

edittext.setText(text);
edittext.setSelection(0);//游標放在開始
edittext.setSelection(text.length);//游標放在最後

6、隱藏和顯示軟鍵盤

1、//隱藏軟鍵盤

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);   

2、//顯示軟鍵盤,控制元件ID可以是EditText,TextView

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控制元件ID, 0);