gpt4 book ai didi

java - 从两个时间选择器中获取总小时数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:07 24 4
gpt4 key购买 nike

有人能告诉我如何从两个 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 >> 并进行区分。这是一个有效的代码 fragment 。

        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
}
}
}

关于java - 从两个时间选择器中获取总小时数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690333/

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