gpt4 book ai didi

android - 将时间选择器添加到日期选择器android

转载 作者:太空狗 更新时间:2023-10-29 13:24:28 27 4
gpt4 key购买 nike

我正在尝试将时间选择器 添加到日期选择器。我是 Java 的初学者,也许有人可以告诉我正确的方法?我希望时间选择器显示在日历 View 的顶部,在我选择日期后,时间选择器会弹出。是否可以 ?在 xml 文件中,只有几个 EditText 字段已在此类中声明。

 public class CompletedWorksActivity extends Activity implements
DatePickerDialog.OnDateSetListener, OnClickListener, OnTimeSetListener {
private TextView text_date;
private Button search;
private EditText from;
private EditText to;
private int yearfrom;
private int monthfrom;
private int dayfrom;
private int hourfrom;
private int minfrom;
private int yearto;
private int monthto;
private int dayto;
private int hourto;
private int minto;
private int hour;
private int min;
private DatePickerDialog date_picker;
private TimePickerDialog time_picker;
private boolean fromEdit;
private boolean toEdit;

@Override
protected void onCreate(Bundle menuinstance) {
super.onCreate(menuinstance);
setContentView(R.layout.completed_works);

Intent intent = getIntent(); // gaunam
User user = (User) intent.getSerializableExtra("user");

from = (EditText) findViewById(R.id.datefrom);
to = (EditText) findViewById(R.id.dateto);
Calendar cal = Calendar.getInstance();
hour = cal.get(Calendar.HOUR_OF_DAY);
min = cal.get(Calendar.MINUTE);
date_picker = new DatePickerDialog(this, this, cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
date_picker.getDatePicker().setCalendarViewShown(true);
date_picker.getDatePicker().setSpinnersShown(false);
time_picker = new TimePickerDialog(this, this, hour, min, true);
from.setOnFocusChangeListener(focusListener);
to.setOnFocusChangeListener(focusListener);
time_picker.set

search = (Button) findViewById(R.id.searchSubmit);

// search.setOnClickListener(searchlistener);
}

@Override
public void onDateSet(DatePicker dp, int y, int m, int d) {
String cheapDate = y + "-" + (m + 1) + "-" + d;
View v = getCurrentFocus();
if (v == from) {
yearfrom = y;
monthfrom = m;
dayfrom = d;
Log.d("year from ", "" + yearfrom + monthfrom + dayfrom);
} else {
yearto = y;
monthto = m;
dayto = d;
Log.d("year to ", "" + yearto + monthto + dayto);
// Toast.makeText(getApplicationContext(),yearto+monthto+dayto ,
// Toast.LENGTH_LONG ).show();
}
if (fromEdit) {
from.setText(cheapDate);
} else {
to.setText(cheapDate);
}

}

private View.OnClickListener searchlistener = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
};

private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
EditText edit = (EditText) v;
int len = edit.getText().toString().length();

if (edit == findViewById(R.id.datefrom)) {
Toast.makeText(getApplicationContext(), "Data nuo ? ",
Toast.LENGTH_LONG).show();
fromEdit = v.getId() == R.id.datefrom;
date_picker.show();
} else {
Toast.makeText(getApplicationContext(), "Data iki ? ",
Toast.LENGTH_LONG).show();
fromEdit = v.getId() == R.id.datefrom;
date_picker.show();

}
}
}

};

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}



}

最佳答案

Android 使用 android.widget.TimePicker 组件 设置时间非常简单。

参见 this Tutorial在这里,您可以看到用户如何使用易于使用的对话框 android.app.TimePickerDialog 选择小时和分钟。

关于android - 将时间选择器添加到日期选择器android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23053515/

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