完全透明的EditText

3
我希望您能够将EditText控件的背景和文本颜色都设置为透明。因此,我在Java代码中使用了以下几行代码:
tempEditText.setTextColor(Color.TRANSPARENT);
tempEditText.setBackgroundColor(Color.TRANSPARENT);

但不幸的是,当我在它上面输入任何字符时,这个tempEditText对象会显示出来。我希望在我输入内容时,它完全不可见。因此,我想让它聚焦,获取一些文本,并且我希望能够在编辑框完全不可见的状态下检索已输入的字符串。我试过将visibility属性设置为invisible,但在这种情况下,EditText无法提供文本...

<EditText
        android:id="@+id/tempEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:textColor="#00000000"
        android:singleLine="true" />

有什么建议吗? 谢谢...


我检查了你的代码,它在我的电脑上完美地运行。我的主要背景是白色。 - Chirag
我的那边不起作用!Main BackGround 的重要性是什么? - androidprogrammer2012
你能发布一张图片吗?其中包含EditText中可见的文本以及XML布局文件。 - Chirag
好的。这是:http://www.imagetoo.com/images/testmqm.jpg - androidprogrammer2012
请上传图片来查看原文。 - Chirag
显示剩余9条评论
6个回答

5

尝试:

android:background="@android:color/transparent"

或者

android:background="@null"

编辑:显示虚拟键盘:

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(exampleView.getWindowToken(), 0);

不是背景,但仍有文本颜色!我希望我的输入字符在可见状态下“不存在”。 - androidprogrammer2012
这听起来有些可疑,为什么要在您的活动顶部放置一个不可见的EditText? - Ljdawson
很简单,兄弟。激活Android虚拟键盘并对其进行操作! - androidprogrammer2012
我已经修改了我的答案,以展示如何显示虚拟键盘。 - Ljdawson
谢谢兄弟,但我还需要一个不可见的EditText。弹出虚拟键盘只是我的一个目标,我确实需要一个在背景和文本两侧都隐藏的精确edittext。 - androidprogrammer2012

1

在你的XML文件中,通过更改android:background="#00000000"android:background="@null"来更改EditText的背景。

对于文本颜色,在XML文件中添加android:textColor="#00000000"


我在XML中设置了这两个,所以背景被移除了,但字符用浅色仍然非常明显! - androidprogrammer2012
@androidprogrammer2012 我无法看到那张图片。请在你的问题中在 Stack Overflow 上上传它。 - Chirag
我在问题正文中添加了图片的URL,但是我无法将图片上传到StackOverflow,因为它不允许我这样做。 - androidprogrammer2012

1
尝试这个,
<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:alpha="0">

或者通过编程,您可以将 alpha 设置到 EditText 中。

editText.setAlpha(0.0f);

注意:用户将无法看到光标!

请更详细地解释你的答案。 - Riyafa Abdul Hameed

0
我在我的 XML 文件中做了这个:
android:textCursorDrawable="@null"
android:textColor="@android:color/transparent"
android:background="@null"

0

EditText的背景属性设置为#bb000000,以使其透明。

如果您使用的是Eclipse,请使用GUI进行操作,或在XML文件中执行此操作:android:background="#bb000000"

IMAGE OF ECLIPSE]


当我按照您的建议操作时,它在设计模式下隐藏了,但是当我运行我的应用程序时,它又重新显示了! - androidprogrammer2012

0
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@null"
            android:id="@+id/search_input"
            app:layout_constraintLeft_toRightOf="@+id/filter"
            app:layout_constraintRight_toLeftOf="@+id/search_button"
            />


           

使用这行代码,它显然会起作用

 android:background="@null" 

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