我知道很多人问过这个问题,但是我似乎无法使它正常工作。
我正在尝试从editTextBirth获取字符串,并将其解析为最新的日期,并由此计算出用户的年龄,将结果投射到editTextAge上(出于个人原因,我希望将其保留为EditText,而不是TextView)。

这是到目前为止我得到的:

public class AddPatientActivity extends AppCompatActivity implements View.OnClickListener{

private static final int DATE_DIALOG_ID = R.layout.birthdaypicker;
private EditText editTextName;
private EditText editTextBirth;
private EditText editTextAge;
private EditText editTextComp;
private SimpleDateFormat dateFormat;
private Calendar c = Calendar.getInstance();
private Calendar now = Calendar.getInstance();
private Button buttonCalc;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_patient);

    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextAge = (EditText) findViewById(R.id.editTextAge);
    editTextBirth = (EditText) findViewById(R.id.editTextBirth);
    editTextComp = (EditText) findViewById(R.id.editTextComp);
    buttonCalc = (Button) findViewById(R.id.buttonCalc);

    dateFormat = new SimpleDateFormat("dd/MM/yyyy");


}

@Override
public void onClick(View view) {
    //if calc button is pressed
    if (view == buttonCalc) {
        String date = editTextBirth.getText().toString();
        try {
            c.setTime(dateFormat.parse(date));
            int age = now.get(Calendar.YEAR) - c.get(Calendar.YEAR);
            if (now.get(Calendar.DAY_OF_YEAR) < c.get(Calendar.DAY_OF_YEAR)) {
                age--;
            }
            String idade = Integer.toString(age);
            editTextAge.setText(idade);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}


不幸的是,当我单击buttonCalc或将输入格式从ddMMyyyy(editTextBirth的显示方式)更改为dd / MM / yyyy时,什么也没有发生。

最佳答案

您创建了onClick事件,但从未将其明确分配给按钮。

在您的按钮声明下面添加以下代码段:

buttonCalc.setOnClickListener(this);


@Pavneet_Singh也正确。您需要比较视图ID,而不仅仅是按钮。

这是您最终的解决方案应为:

public class AddPatientActivity extends AppCompatActivity implements View.OnClickListener{


private static final int DATE_DIALOG_ID = R.layout.birthdaypicker;
private EditText editTextName;
private EditText editTextBirth;
private EditText editTextAge;
private EditText editTextComp;
private SimpleDateFormat dateFormat;
private Calendar c = Calendar.getInstance();
private Calendar now = Calendar.getInstance();
private Button buttonCalc;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_patient);

    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextAge = (EditText) findViewById(R.id.editTextAge);
    editTextBirth = (EditText) findViewById(R.id.editTextBirth);
    editTextComp = (EditText) findViewById(R.id.editTextComp);
    buttonCalc = (Button) findViewById(R.id.buttonCalc);
    buttonCalc.setOnClickListener(this);
    dateFormat = new SimpleDateFormat("dd/MM/yyyy");


}

@Override
public void onClick(View view) {
    //if calc button is pressed
    if (view.getId() == R.id.buttonCalc) {
        String date = editTextBirth.getText().toString();
        try {
            c.setTime(dateFormat.parse(date));
            int age = now.get(Calendar.YEAR) - c.get(Calendar.YEAR);
            if (now.get(Calendar.DAY_OF_YEAR) < c.get(Calendar.DAY_OF_YEAR)) {
                age--;
            }
            String idade = Integer.toString(age);
            editTextAge.setText(idade);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}


}

09-26 02:44