从两个timepicker获得总小时数

时间:2015-09-21 08:12:49

标签: java android 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"/>

2 个答案:

答案 0 :(得分:1)

你可以用(。)点&gt;&gt;计算一天中的小时数来计算你的时间小时和分钟。稍后将其解析为浮动&gt;&gt;并做出分歧。这是一个有效的代码片段。

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

答案 1 :(得分:1)

使用Timepicker Id尝试切换和案例

     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;

                 }