我正在尝试从用户那里获取日期和时间值但是当添加日期和时间文本字段时,没有关于如何写日期或时间的任何内容。寻呼机上有很多东西,日期和来自这些文本字段的时间数据。我获取这些数据并发送Web服务。字段应该看起来像
-/-/- HH:MM:SS
答案 0 :(得分:0)
我没有得到你,你想问用户的时间和日期,并在TextView或EditText中显示它? 你想在textView上放一个面具让它看起来像/ / / - / - ? 或者你想自动获得日期和时间
修改
如果它只是在开头用泼溅显示EditText,你可以简单地做到两种方式
XML:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="----/--/-"
android:id="@+id/et"/>
Java:
EditText et = (EditText) findViewById(R.id.et);
editText.setText(---/--/-);
答案 1 :(得分:0)
你可以这样做:
声明您的EditText
并添加TextWatcher
TextWatcher tw = new TextWatcher() {
private String current = "";
private String ddmmyyyy = "DDMMYYYY";
}
用户输入时
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
String clean = s.toString().replaceAll("[^\\d.]", "");
String cleanC = current.replaceAll("[^\\d.]", "");
int cl = clean.length();
int sel = cl;
for (int i = 2; i <= cl && i < 6; i += 2) {
sel++;
}
//Fix for pressing delete next to a forward slash
if (clean.equals(cleanC)) sel--;
if (clean.length() < 8){
clean = clean + ddmmyyyy.substring(clean.length());
}else{
//This part makes sure that when we finish entering numbers
//the date is correct, fixing it otherwise
int day = Integer.parseInt(clean.substring(0,2));
int mon = Integer.parseInt(clean.substring(2,4));
int year = Integer.parseInt(clean.substring(4,8));
if(mon > 12) mon = 12;
cal.set(Calendar.MONTH, mon-1);
day = (day > cal.getActualMaximum(Calendar.DATE))? cal.getActualMaximum(Calendar.DATE):day;
year = (year<1900)?1900:(year>2100)?2100:year;
clean = String.format("%02d%02d%02d",day, mon, year);
}
clean = String.format("%s/%s/%s", clean.substring(0, 2),
clean.substring(2, 4),
clean.substring(4, 8));
current = clean;
date.setText(current);
date.setSelection(sel < current.length() ? sel : current.length());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
};