如何在Gridview Android中显示timepicker

时间:2018-04-19 11:16:56

标签: android baseadapter android-gridview timepicker

我正在尝试从android中的gridview项目显示TimePicker对话框。

这个想法是当用户点击Griditem的Textview时,需要弹出timepicker对话框。

调整弹出窗口中的时间将在字段中设置时间。我已经开始研究它,但我找不到任何处理我的情况的例子。任何建议将不胜感激。

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="@drawable/bg_student_tile" >

<TextView
    android:id="@+id/tv_booking_dropdate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    android:fontFamily="sans-serif"
    android:textSize="40dp"
    android:textAllCaps="true"
    />
<TextView
    android:id="@+id/tv_booking_droptime"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:fontFamily="sans-serif"
    android:textSize="25dp"
    android:layout_marginTop="30dp"
    android:textAllCaps="true"
    />
<TextView
    android:id="@+id/tv_booking_pickuptime"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:fontFamily="sans-serif"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:textSize="25dp"
    android:textAllCaps="true"
    />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <Button
        android:layout_weight="1"
        android:id="@+id/btnEdit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button_bg_rounded_corners"
        android:textSize="20dp"
        android:text="Change Droptime" />
    <Button
        android:layout_weight="1"
        android:id="@+id/btnAccept"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button_success_bg"
        android:textSize="20dp"
        android:text="Change Pickup Time" />


</LinearLayout>

我的GridAd BaseAdapter如下所示

public class PendingGridViewAdapter extends BaseAdapter {
Context context;
ArrayList<Booking> bookingsList;
private static LayoutInflater inflater = null;
Typeface face;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID=1;
FragmentManager fragmentManager;
public PendingGridViewAdapter(Context context, ArrayList<Booking> bookingsList) {
    this.context = context;
    this.bookingsList = bookingsList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return bookingsList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null)
        convertView = inflater.inflate(R.layout.layout_grid_pending, null);


    //  TextView codeTextView = (TextView) convertView.findViewById(R.id.tv_emp_id);

    final TextView tvDropDate = (TextView) convertView.findViewById(R.id.tv_booking_dropdate);
    final TextView tvDropTime = (TextView) convertView.findViewById(R.id.tv_booking_droptime);
    final TextView tvPickupTime = (TextView) convertView.findViewById(R.id.tv_booking_pickuptime);
    Button btnAccept = (Button) convertView.findViewById(R.id.btnAccept);

    Booking e = new Booking();
    e = bookingsList.get(position);
    String humanReadableDropdate="";

    String dropTime="08:00 AM";
    String pickupTime = "04:00 PM";
    if(!e.getDroptime().equals("00:00")){
        dropTime = e.getDroptime();
    }

    if(!e.getPickuptime().equals("00:00")){
        pickupTime = e.getPickuptime();
    }

    CommonUtils utils = new CommonUtils();
    try {
        humanReadableDropdate=   utils.GetPendingDateInHumanReadableFormat(e.getDropdate());
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    tvDropDate.setText(humanReadableDropdate);
    tvDropTime.setText("DROPPED OFF: " + dropTime);
    tvPickupTime.setText("COLLECTED AT: " + pickupTime);


    try {
        tvDropDate.setTypeface(face, android.R.id.home);
        tvDropTime.setTypeface(face, android.R.id.home);
        tvPickupTime.setTypeface(face, android.R.id.home);


    } catch (Exception ex) {
        // TODO: handle exception
    }

    btnAccept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // showDialog(DATE_DIALOG_ID);
            DialogFragment newFragment = new TimePickerFragment();
        //    newFragment.show(getFragmentManager(),"TimePicker");


        }
    });


    return convertView;
}

public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        //Use the current time as the default values for the time picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        //Create and return a new instance of TimePickerDialog
        return new TimePickerDialog(getActivity(),this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    //onTimeSet() callback method
    public void onTimeSet(TimePicker view, int hourOfDay, int minute){
        //Do something with the user chosen time
        //Get reference of host activity (XML Layout File) TextView widget
        TextView tv = (TextView) getActivity().findViewById(R.id.tv);
        //Set a message for user
        tv.setText("Your chosen time is...\n\n");
        //Display the user changed time on TextView
        tv.setText(tv.getText()+ "Hour : " + String.valueOf(hourOfDay)
                + "\nMinute : " + String.valueOf(minute) + "\n");
    }
}

}

1 个答案:

答案 0 :(得分:0)

将此用于时间选择器对话框, https://github.com/wdullaer/MaterialDateTimePicker

单击您的recyclerview项目时,获取项目的位置,然后显示时间选择器对话框。 并将值设置为所选项目。