Java 从编辑文本中获取int并将其转换为字符串
我遇到了一个交叉问题:我无法获取EditText(int)值,将其转换为字符串,然后在警报对话框中使用它 (我正在验证电话号码。不是高级的!只是简单地确认新用户的真实身份。) 这是我的密码: sms_验证:Java 从编辑文本中获取int并将其转换为字符串,java,android,Java,Android,我遇到了一个交叉问题:我无法获取EditText(int)值,将其转换为字符串,然后在警报对话框中使用它 (我正在验证电话号码。不是高级的!只是简单地确认新用户的真实身份。) 这是我的密码: sms_验证: public class sms_verification extends AppCompatActivity { ImageButton send; EditText text; String myEditValue; public static int
public class sms_verification extends AppCompatActivity {
ImageButton send;
EditText text;
String myEditValue;
public static int phone;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_verification);
send = (ImageButton)findViewById(R.id.imageButton);
text = (EditText)findViewById(R.id.editText);
}
public void AlertDialog(View view) {
myEditValue = text.getText().toString();
phone = Integer.parseInt(myEditValue);
AlertDialog.Builder alertdlg = new AlertDialog.Builder(this);
alertdlg.setMessage("Confirmation")
.setCancelable(false)
.setMessage("Is " + phone +" your phone number?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setContentView(R.layout.sms_verification2);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertdlg.create();
alertDialog.show();
}
}
您应该能够使用:
String.valueOf(phone)
e、 g
对不起,我还不能评论。但是你的电话号码里面有破折号吗?看看你的对话框,你为什么还要解析整数值呢
.setMessage("Is " + myEditValue +" your phone number?")
首先将
String
转换为int
,然后再次将int
转换为String
,这毫无意义
就这么做吧
"Is " + text.getText().toString() +" your phone number?"
您收到了什么错误消息?为什么不直接使用myEditValue变量,它已经是一个字符串,而不是phone变量?
sms\u verification
。那个班同学想。疼。这是Java,不是C,ffs。在你的问题中,“int”的用法是什么?您可以直接使用字符串,而无需将其解析为整数!当parseInt
不起作用时,valueOf
为什么会起作用<实际上,valueOf在内部使用parseInt。区别在于parseInt返回int原语,而valueOf返回整数对象。如果值被连接,java将自动将其转换为字符串,此string.valueOf()
不会有帮助
"Is " + text.getText().toString() +" your phone number?"