我有这个代码,当我选择一个日期时它只在吐司上显示,有没有办法将它插入我的EditText?这是我的代码。
package com.example.kun.carkila;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.Calendar;
public class RegisterActivity extends AppCompatActivity {
EditText etFirstname, etMiddlename, etLastname, etBirthDate, etAddress, etUsername, etPassword;
Spinner spRole;
int year_x,month_x,day_x;
static final int DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
showDialogOnButtonClick();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void showDialogOnButtonClick(){
etBirthDate = (EditText)findViewById(R.id.etBirthDate);
etBirthDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
if(id == DIALOG_ID) {
return new DatePickerDialog(this, dpListener, year_x, month_x, day_x);
}
return null;
}
DatePickerDialog.OnDateSetListener dpListener
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear;
day_x = dayOfMonth;
Toast.makeText(RegisterActivity.this, year_x + "/" + month_x + "/" + day_x,Toast.LENGTH_SHORT).show();
}
};
}
是的,我将EditText设置为可点击以显示datepicker lol。我可以点击我可以点击我的EditText的日期吗?谢谢你的帮助:D
答案 0 :(得分:0)
删除吐司:
Toast.makeText(RegisterActivity.this, year_x + "/" + month_x + "/" + day_x,Toast.LENGTH_SHORT).show();
然后在edittext中设置日期:
etBirthDate.setText(year_x + "/" + month_x + "/" + day_x);
答案 1 :(得分:0)
这应该有效
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear;
day_x = dayOfMonth;
etBirthDate.setText(year_x + "/" + month_x + "/" + day_x);
}
};