Android 编辑文本光标不可见?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21397977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:29:54  来源:igfitidea点击:

Android edit text cursor is not visible?

androidandroid-edittext

提问by Anupama

 <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>  

I am using this code in setBackgroundResource(R.drawable.selfbg);While I am using this code the cursor is not visible in Edittext.

我在使用此代码时,setBackgroundResource(R.drawable.selfbg);在使用此代码时,光标在 Edittext 中不可见。

回答by Luc

Did you try?

你试过了吗?

Setting the android:textCursorDrawableattribute to @null should result in the use of android:textColoras the cursor color.

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

回答by M D

try to add below into EditText:

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

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

回答by vipul mittal

try:

尝试:

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

It requires API-12 and above though.

不过它需要 API-12 及更高版本。

回答by Ravv

<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>

回答by ask4solutions

try this

尝试这个

In your EditText, use the property:

在您的 EditText 中,使用以下属性:

android:textCursorDrawable="@drawable/black_cursor"

and add the drawable black_cursor.xml to your resources, as follows:

并将可绘制的 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>

回答by ask4solutions

If you want this done through java code only try this

如果您想通过 java 代码完成此操作,请尝试此操作

In TextView class you can find the code below

在 TextView 类中,您可以找到以下代码

case com.android.internal.R.styleable.TextView_textCursorDrawable:
    mCursorDrawableRes = a.getResourceId(attr, 0);
    break;

but the method textCursorDrawable() is only existed in R.attr, if you wanna set this attribute, you can only call the construct method below by including a editText in XML file.

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

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

回答by Jason Mills

MainActivity

主要活动

@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>

content_main

content_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>

As you can see, my EditText is not in activity_main, but in content_main, which I included in activity_main anyway hoping it would work the same, but NO... It doesn't work like that unfortunately. So I change my activity_main like this and it works like a charm.

如您所见,我的 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>

回答by Jason Mills

i think that you cant "setTextCursorDrawable" by code .

我认为你不能通过代码“setTextCursorDrawable”。

ive looked into the Android documentation of EditTextand TextViewand there is no such method .

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

so you have to do it in the xml layout .

所以你必须在 xml 布局中进行。