Java 在编辑文本中手动输入日期
我想在edittext字段中手动(即通过数字键盘)输入dd-mm-yyyy格式的日期。还可以为另一次计算分配三个整数变量,即日、月、年,使前两个字符(即索引0、1)成为日变量,然后自动添加分隔符(“-”或“/”)接下来的两位数字应该是月份变量,然后是一个自动分隔符,如前一位和最后一位。日期和月份应该显示为两位数字,前导零(如有必要)。如果用户在索引零(0)的日期字段中输入4-9它会自动将其转换为两位数并移动到月份字段,对于2-9月份,也会将该两位数转换为年份。最后,当输入过程完成时,将光标移动到下一个edittext字段。 我为我糟糕的英语道歉。 任何形式的帮助都将不胜感激。Java 在编辑文本中手动输入日期,java,android,android-edittext,user-input,Java,Android,Android Edittext,User Input,我想在edittext字段中手动(即通过数字键盘)输入dd-mm-yyyy格式的日期。还可以为另一次计算分配三个整数变量,即日、月、年,使前两个字符(即索引0、1)成为日变量,然后自动添加分隔符(“-”或“/”)接下来的两位数字应该是月份变量,然后是一个自动分隔符,如前一位和最后一位。日期和月份应该显示为两位数字,前导零(如有必要)。如果用户在索引零(0)的日期字段中输入4-9它会自动将其转换为两位数并移动到月份字段,对于2-9月份,也会将该两位数转换为年份。最后,当输入过程完成时,将光标移动到
提前感谢。我想您必须生成一个新视图,扩展EditText并覆盖onkeyup方法。
每次输入密钥时,您都可以获取文本,对其进行分析并根据需要进行更改。您可以使用此项。某些边缘情况不可处理。请执行这些操作
private static final char SEPERATOR = '-';
private void automateDateEntry() {
dateEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() > 10) {
charSequence = charSequence.subSequence(0, 10);
dateEditText.removeTextChangedListener(this);
dateEditText.setText(charSequence);
dateEditText.addTextChangedListener(this);
}
}
@Override
public void afterTextChanged(Editable editable) {
String currText = editable.toString();
if(currText.isEmpty())
return;
int separatorCount = getOcurrence(currText, SEPERATOR);
if (currText.charAt(currText.length() - 1) == SEPERATOR) {
return;
}
int lastSeperatorPos = currText.lastIndexOf(SEPERATOR);
String previousTxt = null;
if (lastSeperatorPos > -1) {
previousTxt = currText.substring(0, lastSeperatorPos + 1);
Log.d("curr text", currText);
currText = currText.substring(lastSeperatorPos + 1);
}
if (!currText.isEmpty()) {
switch (separatorCount) {
case 0:
int date = Integer.parseInt(currText);
if (date < 0 || date > 31) {
dateEditText.setError("Enter proper date");
return;
}
if (currText.length() == 2 || date >= 4) {
currText = String.format(Locale.getDefault(), "%02d", date);
currText += SEPERATOR;
}
break;
case 1: int month = Integer.parseInt(currText);
if (month < 0 || month > 31) {
dateEditText.setError("Enter proper month");
return;
}
if (currText.length() == 2 || month >= 2) {
currText = String.format(Locale.getDefault(), "%02d", month);
currText += SEPERATOR;
}
break;
case 2: int year = Integer.parseInt(currText);
if (year < 0 ) {
dateEditText.setError("Enter proper year");
return;
}
break;
}
}
if(previousTxt != null){
currText = previousTxt + currText;
}
dateEditText.removeTextChangedListener(this);
dateEditText.setText(currText);
int textLength = currText.length();
dateEditText.setSelection(textLength, textLength);
dateEditText.addTextChangedListener(this);
}
});
}
private int getOcurrence(String string, char ch) {
int count = 0;
int len = string.length();
for (int i = 0; i < len; i++) {
if (string.charAt(i) == ch)
++count;
}
return count;
}
private静态最终字符分隔符='-';
私有void automateDateEntry(){
dateEditText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i1,int i2){
if(charSequence.length()>10){
charSequence=charSequence.子序列(0,10);
dateEditText.removeTextChangedListener(此);
dateEditText.setText(字符序列);
dateEditText.addTextChangedListener(此);
}
}
@凌驾
public void PostTextChanged(可编辑){
字符串currText=editable.toString();
if(currText.isEmpty())
返回;
int separatorCount=getOcurrence(currText,分隔符);
if(currText.charAt(currText.length()-1)=分隔符){
返回;
}
int lastspeparatorpos=currText.lastIndexOf(分隔符);
字符串previousTxt=null;
如果(LastSeparatorPos>-1){
previousTxt=currText.substring(0,LastSeparatorPos+1);
Log.d(“当前文本”,当前文本);
currText=currText.substring(lastSeparatorPos+1);
}
如果(!currText.isEmpty()){
开关(分离器计数){
案例0:
int date=Integer.parseInt(currText);
如果(日期<0 | |日期>31){
dateEditText.setError(“输入正确的日期”);
返回;
}
如果(currText.length()=2 | |日期>=4){
currText=String.format(Locale.getDefault(),“%02d”,日期);
currText+=分隔符;
}
打破
案例1:int-month=Integer.parseInt(currText);
如果(月<0 | |月>31){
dateEditText.setError(“输入正确的月份”);
返回;
}
如果(currText.length()=2 | |月>=2){
currText=String.format(Locale.getDefault(),“%02d”,月);
currText+=分隔符;
}
打破
案例2:int year=Integer.parseInt(currText);
如果(年份<0){
dateEditText.setError(“输入正确的年份”);
返回;
}
打破
}
}
if(previousTxt!=null){
currText=previousTxt+currText;
}
dateEditText.removeTextChangedListener(此);
dateEditText.setText(currText);
int textLength=currText.length();
dateEditText.setSelection(textLength,textLength);
dateEditText.addTextChangedListener(此);
}
});
}
private int getOcurrence(字符串、字符){
整数计数=0;
int len=string.length();
对于(int i=0;i
非常感谢@Debanjan。这非常有帮助。但是有一个小问题。当我试图在文本视图中打印变量时,所有变量的值都为零。有什么建议吗?哪些变量?大多数是整数,所以要打印它们,应该使用String.valueOf(month),像这样。接受或投票,如果这符合你的目的。欢迎。变量打印问题解决了。实际上,我必须将打印代码放在onClickListener中。现在,如果我想编辑一个特定的变量,你能帮我解决这个问题吗@Debanjan。我在这个领域非常新手。我如何接受你的建议?我必须以“注释”的形式发表评论吗接受?谢谢你的时间和合作。我不明白这个问题,只需点击答案旁边的勾号。