有人能告诉我如何从两个 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"/>

最佳答案

您可以计算一天中的小时数,用 (.) 点 >> 将您的时间小时数和分钟数连接起来,然后将其解析为浮点数 >> 并有所不同。这是一个工作代码 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/

10-10 11:51