android中EditText光标问题

如图所示,点击EditText,EditText获得焦点,显示删除按钮;点击界面其他位置,EditText失去焦点

点击界面其他位置,EditText失去焦点,mLayout是整个布局
mLayout.setOnTouchListener(new View.OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { mLayout.setFocusable(true); mLayout.setFocusableInTouchMode(true); mLayout.requestFocus(); return false; } });
EditText获取焦点事件 mNumET.setOnFocusChangeListener(
new View.OnFocusChangeListener(){ @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { if (!TextUtils.isEmpty(mNumET.getText())) { mDeleteButton.setVisibility(View.VISIBLE); } }else { mDeleteButton.setVisibility(View.GONE); } } });
  <EditText
            android:id="@+id/et_num"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="18dp"
            android:layout_marginRight="18dp"
            android:layout_toLeftOf="@id/btn_delete"
            android:background="@null"
            android:inputType="number"
            android:maxLength="11"
            android:paddingLeft="1dp"
            android:text="13416359156"
            android:textColor="#000000"
            android:textCursorDrawable="@drawable/cursor_drawable"
            android:textSize="17dp" />

cursor_drawable设置为:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/black" />
    <size android:width="1px"/>
</shape>

注意 android:textCursorDrawable="@drawable/cursor_drawable",如果用android:textCursorDrawable="@null"可能会出现双光标

如果EditText默认不获取焦点,则需要将其parent布局设置为

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="#FFFFFF" >

 

posted @ 2015-07-07 10:34  jch19881224  阅读(1919)  评论(0)    收藏  举报