简体   繁体   English

尝试将数字设置为EditText中的文本

[英]Trying to set number as text in EditText

I am having trouble setting a double in an edittext and I cannot get rid of the error. 我无法在edittext中设置double,并且无法摆脱错误。 What am I doing wrong? 我究竟做错了什么?

Here is what I have: 这是我所拥有的:

    final EditText somedouble_et = (EditText)findViewById(R.id.somedouble_search);
    somedouble_et.setText(doubleNumber.toString(), TextView.BufferType.EDITABLE);

The error I am getting is that is doesn't recognize TextView as a class. 我得到的错误是不能将TextView识别为类。 Is there an error in my xml? 我的xml中有错误吗?

<EditText android:layout_width = "fill_parent"
    android:layout_height = "fill_parent"
    android:id="@+id/somedouble"
    android:inputType="numberSigned"
    android:hint = ""
    android:text = "@+id/somedouble_search"/>

It does not seem to matter what id I use. 我使用的ID似乎无关紧要。

Should the first line of your code not be 代码的第一行应该不是

final EditText somedouble_et = (EditText)findViewById(R.id.somedouble);

and in the xml, android:text should not be "@+id" anything! 并且在xml中,android:text不应为“ @ + id”!

Make sure you have imported TextView at the top of your code. 确保已在代码顶部导入了TextView。

In your XML file, the android:id field should be as follows: 在您的XML文件中,android:id字段应如下所示:

android:id="@+id/somedouble_search"

The text field should either be hardcoded ("mytext") or refer to a string resource (@string/mystringresource). 文本字段应为硬编码(“ mytext”)或引用字符串资源(@ string / mystringresource)。

Hardcoded 硬编码

android:text = "Hardcoded text"

Referencing something from strings.xml 引用来自strings.xml的内容

android:text="@string/mystringresource"

The following piece of code is searching for an EditText with the id of somedouble_search in your XML file, yet your id is defined as android:id="@+id/somedouble" 以下代码段在XML文件中搜索ID为somedouble_search的EditText,但您的ID被定义为android:id =“ @ + id / somedouble”

final EditText somedouble_et = (EditText)findViewById(R.id.somedouble_search);

Remember 记得

To reference any object defined in an XML, you reference it by its id defined in the xml - NOT by its text attribute. 要引用XML中定义的任何对象,请通过xml中定义的ID而不是其text属性来引用。 The text attribute only defines what text should be displayed. text属性仅定义应显示的文本。

For reference, please see this link. 仅供参考,请参阅链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM