gpt4 book ai didi

java - 将日期从 EditText 解析到 Calendar 并从中计算年龄

转载 作者:行者123 更新时间:2023-12-02 02:22:26 24 4
gpt4 key购买 nike

我知道很多人问过这个问题,但我似乎无法让它发挥作用。我试图从 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 也是正确的。您需要比较 View 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();
}
}

}

}

关于java - 将日期从 EditText 解析到 Calendar 并从中计算年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48303312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com