如何获得毫秒的时间输入?

发布于 2025-02-06 17:49:39 字数 334 浏览 1 评论 0原文

private long START_TIME_IN_MILLIS;
private long TimeLeftInMillis = START_TIME_IN_MILLIS;

我已经声明了这些变量,该变量是为了存储来自EditText的输入,但是在我的increateview中,我有此行

start_time_in_in_millis = edtinserttime.getText();

我一个错误,如何在毫秒中获取时间输入并将其存储在start_time_in_millis中?

private long START_TIME_IN_MILLIS;
private long TimeLeftInMillis = START_TIME_IN_MILLIS;

i have declared these variables, which is meant to store an input from an EditText, but in my OnCreateView i have this line

START_TIME_IN_MILLIS = edtInsertTime.getText();

but it gives me an error, how do I get the input of time in milliseconds and store it in START_TIME_IN_MILLIS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2025-02-13 17:49:39

您没有发布您收到的错误,这将是有帮助的,因为这可能是一百个原因。

只要查看此代码,您就有多个类型的变量(是数字类型),以及您从edtinsettime.getText()? ,根据文档的类型“可编辑”,并且您无法将可编辑的对象存储在长变量中。

public Editable getText ()
Returns Editable - The text displayed by the text view.

要从中获得一个长的值,您首先需要从此Edittext获得一个字符串,然后将其转换为长格式,然后将其分配给变量。

检查代码将是。

START_TIME_IN_MILLIS = Long.valueOf(edtInsertTime.getText().toString());

分解。

字符串text = edtinserttime.getText()。tostring()
询问此问题的方法,可以将其返回该值的字符串值

通过将字符串解析为长

Long.parseLong(text)

Long.valueOf(text)

,然后将字符串解析为长值,这意味着您对Java中类型的工作方式没有基本的了解,因此阅读有关 https://wwwww.baeldung.com/java-primitives

You didn't post the error you received, and that would be helpful since it can be of hundred reasons..

Just looking at this code, you have variable of type long, ( which is a numeric type ), and what you get from edtInsetTime.getText()? , a type "Editable" according to the documentation, and you can't store Editable object in a long variable.

https://developer.android.com/reference/android/widget/EditText

public Editable getText ()
Returns Editable - The text displayed by the text view.

To get a long value from it, you first need to get a String from this EditText, then convert it to a long format, and then assign it to the variable.

Examplary code would be.

START_TIME_IN_MILLIS = Long.valueOf(edtInsertTime.getText().toString());

Breaking it down.

String text = edtInsertTime.getText().toString()
returns the String value of an edittext

That value then can be converted to a long value by parsing the String to long, with methods like

Long.parseLong(text)

or

Long.valueOf(text)

Asking this question means you don't have basic understanding of how types works in java, so feel free to read about it https://www.baeldung.com/java-primitives

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文