发送长信息到共享首选项 - Android

发布于 2025-01-07 01:56:48 字数 1036 浏览 6 评论 0原文

我开发了一个对话框,在应用程序首次运行时提示用户输入他/她的电话号码,但我在将其添加到共享首选项时遇到问题。我已经设置了 SP,并且可以从另一个视图正常工作(添加/编辑信息),并且我能够提交用户 ID。问题是我使用 Long 作为电话号码而不是字符串,所以我不断收到错误,并且似乎无法弄清楚我需要做什么。我认为可编辑也可能是一个问题。我尝试将 getText() 更改为 getLong() ,但这似乎不起作用,而且我无法将其转换为字符串,因为电话号码在我的 SP 中被识别为 Long 。我如何才能将 Long 提交给共享偏好?谢谢。

这是我的代码:

    if(phone == 0) {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Please Enter Your Phone Number");
        alert.setMessage("You must enter your phone number in order to use this application");

        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        Editable value = input.getText();

        Editor editor = shared.edit();
        editor.putLong("PHONE", value); //wants me to change putLong to putString
        editor.commit();
         }
        });
        alert.show();     
    }

I've developed a dialog box that prompts a user to enter his/her phone number when the app first runs but I'm having trouble adding it to the shared preferences. I have the SP set up and working correctly from another view (to add/edit info) and I was able to commit a user id. The problem is that I'm using a Long for the phone number instead of a string, so I keep getting errors and can't seem to figure out what I need to do. I think the Editable may also be an issue. I've tried changing getText() to getLong() but that doesn't seem to work and I can't convert it to a string because the phone number is recognized as a Long in my SP. How can I commit the Long to the shared preferences? Thanks.

Here is my code:

    if(phone == 0) {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Please Enter Your Phone Number");
        alert.setMessage("You must enter your phone number in order to use this application");

        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        Editable value = input.getText();

        Editor editor = shared.edit();
        editor.putLong("PHONE", value); //wants me to change putLong to putString
        editor.commit();
         }
        });
        alert.show();     
    }

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

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

发布评论

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

评论(3

ζ澈沫 2025-01-14 01:56:48

Alex,您可以执行以下操作将 Long 放入 SP:

String value = input.getText().toString();

Editor editor = shared.edit();
editor.putLong("PHONE", Long.valueOf(value));
editor.commit();

Alex, you could do the following to put a Long into the SP:

String value = input.getText().toString();

Editor editor = shared.edit();
editor.putLong("PHONE", Long.valueOf(value));
editor.commit();
○闲身 2025-01-14 01:56:48

更改

可编辑值= input.getText();

String value = input.getText().toString();

将为您提供文本框的字符串值。

现在要储存。

当且仅当您确定(并且您已验证)此处的 value 仅包含数字时,请执行以下操作:

editor.putLong("PHONE", Long.valueOf(value));

仍然我更喜欢

editor.putString("PHONE",value);

,并且您也可以随时使用字符串检索它,然后将其更改为 Long按要求...

change

Editable value = input.getText();

to

String value = input.getText().toString();

This will give you the String value of the Textbox.

Now to store.

If and only if you are sure(and you have validated) that the value here only contains digits, do:

editor.putLong("PHONE", Long.valueOf(value));

still i would prefer

editor.putString("PHONE",value);

and you can retrieve it with string anytime too,and then change it to Long as required...

昵称有卵用 2025-01-14 01:56:48

对不起,我的英语

你没有将 String 值转换为 Long

使用 parseLong(String s, int radix) 方法,如下所示:parseLong(value, 10)

sorry for my english

you doesn't convert String value into Long

use parseLong(String s, int radix) method like this: parseLong(value, 10)

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