java - ImageView 上的透明背景 EditText 不会显示光标

标签 java android android-layout view android-edittext

因此,我希望用户能够在我设置为 ImageView 的图像上键入内容。图像已设置并可运行。当用户单击图像时,他们单击的位置会显示一个编辑文本框。但编辑文本的白色背景遮挡了背景中的图像。因此,我将编辑文本背景设置为透明:

    editTextOne.setBackgroundColor(Color.TRANSPARENT);

这适用于使编辑文本背景透明。但是,现在光标也不会显示,除非开始输入。

所以,我要问的是是否有办法使 EditText 背景透明,但仍然让光标显示并闪烁?任何帮助将不胜感激,谢谢!

代码:

    public boolean onTouch(View view, MotionEvent event){
            if (type == true){
                touchX = (int) event.getX();
                touchY = (int) event.getY();
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) editTextOne.getLayoutParams();
                params.leftMargin = touchX;
                params.topMargin = touchY;
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                editTextOne.setLayoutParams(params);
                editTextOne.setVisibility(View.VISIBLE);
                editTextOne.bringToFront();
                editTextOne.setBackgroundColor(Color.TRANSPARENT);
                editTextOne.requestFocus();
                editTextOne.setCursorVisible(true);
            }//end of if statement
            return true;
        }//end of onTouch

XML:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/relative" >

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toLeftOf="@+id/load"
    android:layout_alignParentBottom="true"
    android:layout_margin="10dp"
    android:text="C"
    android:textStyle="bold"
    android:textSize="20dp"
    android:id="@+id/camera"
    android:onClick="camera"/>

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toLeftOf="@+id/view"
    android:layout_alignParentBottom="true"
    android:layout_margin="10dp"
    android:id="@+id/load"
    android:text="L"
    android:textStyle="bold"
    android:textSize="20dp"
    android:onClick="load"/>

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="10dp"
    android:text="v"
    android:id="@+id/view"
    android:textStyle="bold"
    android:textSize="20dp"
    android:onClick="view"/>

<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toLeftOf="@+id/camera"
    android:layout_alignParentBottom="true"
    android:layout_margin="10dp"
    android:text="T"
    android:textStyle="bold"
    android:textSize="20dp"
    android:id="@+id/text"
    android:onClick="text"/>

<TextView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@null"
    android:visibility="invisible"
    android:id="@+id/textone"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:inputType="textAutoCorrect"
    android:singleLine="true"
    android:id="@+id/edittextone"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:id="@+id/picture"/>

</RelativeLayout>  

编辑:我不确定为什么光标不会立即显示,但我发现按下空格键后光标就可见。因此,为了解决我的问题,我使用了:

     editTextOne.setText(" ");

这将放置一个空格作为初始文本,因此显示光标。我希望这可以帮助遇到类似问题的其他人。

最佳答案

您可以在xml文件中更改edittext属性android:background="@null",而不是在代码文件中设置,这对我有用。我希望它对您有用。

关于java - ImageView 上的透明背景 EditText 不会显示光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079206/

相关文章:

java - 如何删除双向链表中的尾部?

java - 将基于服务器的产品的 Java 开发迁移到 64 位有什么主要优势吗?

android - 在 Activity 中禁用或阻止多点触控

android - Facebook Messenger 如何绘制聊天头? (安卓)

java - 给定目标的 5 的倍数的总和

java - 添加公共(public)类以使用 xpath 和 Java Selenium 进行单击

android - 使用 DrawerLayout 时发生未知错误?

java - 如何获取字符串并转换为 fragment 名称以启动类?

Android ProgressDialog "Spinner"消失(不显示)

java - 仅在必要时在特定位置添加换行符