发送长信息到共享首选项 - Android
我开发了一个对话框,在应用程序首次运行时提示用户输入他/她的电话号码,但我在将其添加到共享首选项时遇到问题。我已经设置了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Alex,您可以执行以下操作将 Long 放入 SP:
Alex, you could do the following to put a Long into the SP:
更改
可编辑值= input.getText();
这
将为您提供文本框的字符串值。
现在要储存。
当且仅当您确定(并且您已验证)此处的
value
仅包含数字时,请执行以下操作:仍然我更喜欢
,并且您也可以随时使用字符串检索它,然后将其更改为 Long按要求...
change
Editable value = input.getText();
to
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:still i would prefer
and you can retrieve it with string anytime too,and then change it to Long as required...
对不起,我的英语
你没有将 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)