1. 程式人生 > >光標設置、hint設置

光標設置、hint設置

bre err eight ole ase string num nco enter

1.在edittext編輯框中調用屬性。
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="5dp"
android:layout_marginRight="30dp"
android:background="@null"
android:textColorHint="@android:color/white" // 提示文字顏色設置
android:textCursorDrawable="@drawable/cursor" // 光標顏色、大小等設置
android:gravity="center_vertical"
android:hint="@string/hint_phone_number"
android:singleLine="true"
/>

2.創建一個drawable資源文件來設置,也可以直接用顏色設置。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid
android:color="@android:color/white"></solid>
<size android:width="1dp"></size>
</shape>
</item>
</layer-list>

3.監聽edittext的焦點變化,獲得焦點時將光標設置到最後的位置;
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()){
case R.id.user_name_or_phone_number:
if (hasFocus){
String content=userName.getText().toString(); //獲取編輯框的內容
userName.setSelection(content.length()); // 將光標設置的最後的位置
}
break;
}
}

光標設置、hint設置