有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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();    
    }
}

共 (3) 个答案

  1. # 1 楼答案

    您应该能够使用:

    String.valueOf(phone)
    

    例如

    .SetMessage("Is " + String.valueOf(phone) + " your phone number")
    
  2. # 2 楼答案

    抱歉,我还不能评论。但你的电话号码里有破折号吗?看看你的对话框,你为什么还要解析整数值呢

    .setMessage("Is " + myEditValue +" your phone number?")
    
  3. # 3 楼答案

    首先将String转换为int,然后再次将int转换为String,这没有意义

    就这么做

    "Is " + text.getText().toString() +" your phone number?"