从两个时间选择器中获取总小时数
get total hours from two timepicker
谁能告诉我如何从两个 TimePicker 获取总小时数?这是可能的,因为这两个 timepicker 实际上是从同一个函数获取的。
WorkDetails.java
EditText TimeIn = (EditText) findViewById(R.id.editText6);
EditText TimeOut = (EditText) findViewById(R.id.editText7);
TimeIn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TimePick time = new TimePick(v);
FragmentTransaction ft = getFragmentManager().beginTransaction();
time.show(ft, "TimePicker");
}
}
});
TimeOut.setOnFocusChangeListener(new View.OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus) {
if (hasFocus) {
TimePick time = new TimePick(v);
FragmentTransaction ft = getFragmentManager().beginTransaction();
time.show(ft, "TimePicker");
}
}
});
TimePick.java
public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
EditText time;
public TimePick(View view)
{
time=(EditText)view;
}
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c= Calendar.getInstance();
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
}
}
workdetails.xml
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:id="@+id/editText6"
android:layout_x="147dp"
android:layout_y="170dp"
android:hint="Select Time"
android:clickable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time In"
android:id="@+id/textView7"
android:layout_x="23dp"
android:layout_y="184dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time out"
android:id="@+id/textView8"
android:layout_x="23dp"
android:layout_y="247dp" />
<EditText
android:layout_width="143dp"
android:layout_height="wrap_content"
android:id="@+id/editText7"
android:layout_x="147dp"
android:layout_y="241dp"
android:hint="Select Time"/>
您可以计算一天中的小时数,用 (.) 点连接您的时间小时数和分钟数 >> 稍后将其解析为浮点数 >> 并进行区分。这是一个有效的代码片段。
float end = Float.parseFloat("21.00");
float start = Float.parseFloat("10.00");
System.out.println(end-start);
改变你的方法,如:
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
time.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
}
稍后从 editText.gettext();
中获取值
编辑:
使您的 TimeIn
editText 变量成为静态变量,并在像这样设置超时时获取它的值..
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
time.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
if(time.getId==R.id.editText7){
if(YourActivity.TimeIn !=null){
String timeIn = YourActivity.TimeIn.getText().toString();
float end = Float.parseFloat(time.getText().toString);
float start = Float.parseFloat(timeIn);
System.out.println(end-start);
// do what you need with your calculated hours
}
}
}
使用 Timepicker Id 尝试 Switch 和 Case
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
int start,end,remains;
switch(view.getId()){
case R.id.editText6:
time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
start=hourofDay;
break;
case R.id.editText7:
time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
end=hourofDay;
break;
}
//Use can declare remains globlally and use where you want
remain=end-start;
}
谁能告诉我如何从两个 TimePicker 获取总小时数?这是可能的,因为这两个 timepicker 实际上是从同一个函数获取的。
WorkDetails.java
EditText TimeIn = (EditText) findViewById(R.id.editText6);
EditText TimeOut = (EditText) findViewById(R.id.editText7);
TimeIn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TimePick time = new TimePick(v);
FragmentTransaction ft = getFragmentManager().beginTransaction();
time.show(ft, "TimePicker");
}
}
});
TimeOut.setOnFocusChangeListener(new View.OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus) {
if (hasFocus) {
TimePick time = new TimePick(v);
FragmentTransaction ft = getFragmentManager().beginTransaction();
time.show(ft, "TimePicker");
}
}
});
TimePick.java
public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
EditText time;
public TimePick(View view)
{
time=(EditText)view;
}
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c= Calendar.getInstance();
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
}
}
workdetails.xml
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:id="@+id/editText6"
android:layout_x="147dp"
android:layout_y="170dp"
android:hint="Select Time"
android:clickable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time In"
android:id="@+id/textView7"
android:layout_x="23dp"
android:layout_y="184dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time out"
android:id="@+id/textView8"
android:layout_x="23dp"
android:layout_y="247dp" />
<EditText
android:layout_width="143dp"
android:layout_height="wrap_content"
android:id="@+id/editText7"
android:layout_x="147dp"
android:layout_y="241dp"
android:hint="Select Time"/>
您可以计算一天中的小时数,用 (.) 点连接您的时间小时数和分钟数 >> 稍后将其解析为浮点数 >> 并进行区分。这是一个有效的代码片段。
float end = Float.parseFloat("21.00");
float start = Float.parseFloat("10.00");
System.out.println(end-start);
改变你的方法,如:
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
time.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
}
稍后从 editText.gettext();
编辑:
使您的 TimeIn
editText 变量成为静态变量,并在像这样设置超时时获取它的值..
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
time.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
if(time.getId==R.id.editText7){
if(YourActivity.TimeIn !=null){
String timeIn = YourActivity.TimeIn.getText().toString();
float end = Float.parseFloat(time.getText().toString);
float start = Float.parseFloat(timeIn);
System.out.println(end-start);
// do what you need with your calculated hours
}
}
}
使用 Timepicker Id 尝试 Switch 和 Case
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
int start,end,remains;
switch(view.getId()){
case R.id.editText6:
time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
start=hourofDay;
break;
case R.id.editText7:
time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
end=hourofDay;
break;
}
//Use can declare remains globlally and use where you want
remain=end-start;
}