为什么它没有获得当前的EditText值

时间:2016-10-06 16:46:55

标签: android

我正在做与登录相关的基本演示,更新用户
我使用登录页面中的用户ID加载用户的信息以更新页面

 username = (EditText) findViewById(R.id.txtUsername);
 age = (EditText) findViewById(R.id.txtAge);
 weight  = (EditText) findViewById(R.id.txtWeight);
 height = (EditText) findViewById(R.id.txtHeight);
 username.setText(db_username);
 age.setText(db_age);
 weight.setText(db_weight);
 height.setText(db_height);

并更新按钮

update.setOnClickListener(new View.OnClickListener() {
        int sweight = Integer.parseInt(weight.getText().toString());
        double sheight = Double.parseDouble(height.getText().toString());
        int sage = Integer.parseInt(age.getText().toString());
        @Override
        public void onClick(View v) {
            if (weight.equals("")) {
                Toast.makeText(getApplicationContext(), "Please input weight!",
                        Toast.LENGTH_LONG).show();
                return;
            }else if(height.equals("")){
                Toast.makeText(getApplicationContext(), "Please input height!",
                        Toast.LENGTH_LONG).show();
                return;
            }else if(age.equals("")){
                Toast.makeText(getApplicationContext(), "Please input age!",
                        Toast.LENGTH_LONG).show();
                return;
            }else{
                dbHandler.updateUser(sid,sweight,sheight,sage);
                Toast.makeText(getApplicationContext(),
                        "User's information updated! ", Toast.LENGTH_LONG)
                        .show();
                Toast.makeText(getApplicationContext(),
                        sid+sweight+sheight+sage+" and test", Toast.LENGTH_LONG)
                        .show();
                Intent i = new Intent(UserUpdate.this, MainActivity.class);
                startActivity(i);
                finish();

            }
        }
    });

考试:体重:70,身高:1,65,年龄:21岁 当我改为:体重:50,身高:1,65,年龄:21岁 第二次Toast告诉我重量:70但不是50左右

2 个答案:

答案 0 :(得分:2)

您需要在click方法中移动这三个前三行,而不是将它们声明为匿名类的成员变量。

    int sweight = Integer.parseInt(weight.getText().toString());
    double sheight = Double.parseDouble(height.getText().toString());
    int sage = Integer.parseInt(age.getText().toString());

    @Override
    public void onClick(View v) {

        // Move them here

答案 1 :(得分:0)

把这个:

    int sweight = Integer.parseInt(weight.getText().toString());
    double sheight = Double.parseDouble(height.getText().toString());
    int sage = Integer.parseInt(age.getText().toString());

在onclick方法中,之前 - > if(weight.equals(""))