Android编辑框光标不可见?

16
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <solid android:color="#ffffff" />    
 <stroke android:width="1dp" 
  android:color="#000000" />
 </shape>  

我正在使用这段代码:setBackgroundResource(R.drawable.selfbg); 但是在使用这段代码时,EditText中的光标不可见。

9个回答

46

您尝试过吗?

android:textCursorDrawable属性设置为@null应该会使用android:textColor作为光标颜色。


10

尝试将以下内容添加到EditText中:

 android:textCursorDrawable="@null"
 android:background="@null"

7
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/color_cursor"
    android:background="@drawable/R.drawable.selfbg"/>


Than create drawalble xml: color_cursor

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

6

尝试:

 <item name="android:textCursorDrawable">@null</item>

不过,这需要API-12及以上版本。


1

主活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText editText = findViewById(R.id.editText);
    editText.requestFocus();
    editText.setSelection(0);
}

activity_main

 <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <include
            layout="@layout/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
 </LinearLayout>

内容主要

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
 </LinearLayout>

正如您所看到的,我的EditText不在activity_main中,而是在content_main中,我仍然将其包含在activity_main中,希望它能够像以前一样工作,但是很遗憾,它并不是这样的。因此,我像这样更改了我的activity_main,现在它可以完美地工作了。

**activity_main**

 <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
 </LinearLayout>

1
尝试一下。
在您的EditText中,使用以下属性:
android:textCursorDrawable="@drawable/black_cursor"

请将可绘制的 black_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="0dp" />
</shape>

0
如果您只想通过Java代码完成此操作,请尝试以下方法:
在TextView类中,您可以找到以下代码:
case com.android.internal.R.styleable.TextView_textCursorDrawable:
    mCursorDrawableRes = a.getResourceId(attr, 0);
    break;

但是方法textCursorDrawable()仅存在于R.attr中,如果您想设置此属性,只能通过在XML文件中包含editText来调用下面的构造方法。

public EditText(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.editTextStyle);
}

0

我认为你无法通过代码来设置 "setTextCursorDrawable"。

我查看了Android文档中的EditTextTextView,并没有这样的方法。

因此,你必须在XML布局中完成它。


0

我认为你必须在XML布局中的EditText中包括requestFocus,这样它才能工作。相反,我看到很多没有包含requestFocus的例子,但是我测试过它们不起作用,只有当我添加requestFocus时才能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接