java从编辑文本中获取int并将其转换为字符串
我遇到了一个问题:我无法获取EditText(int)值,将其转换为字符串,然后在警报对话框中使用它
(我正在做一个电话号码验证。不是高级的!只是简单地确认新用户的真实身份。) 这是我的代码:
sms_验证:
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();
}
}
# 1 楼答案
您应该能够使用:
例如
# 2 楼答案
抱歉,我还不能评论。但你的电话号码里有破折号吗?看看你的对话框,你为什么还要解析整数值呢
# 3 楼答案
首先将
String
转换为int
,然后再次将int
转换为String
,这没有意义就这么做