簡體   English   中英

如何在Gridview Android中顯示時間選擇器

[英]How to show timepicker in Gridview Android

我正在嘗試從android中的gridview項目顯示TimePicker對話框。

這個想法是當用戶單擊Griditem的Textview時,需要彈出一個時間選擇對話框。

在彈出窗口中調整時間將在字段中設置時間。 我曾經研究過它,但是找不到適合我情況的示例。 任何建議將不勝感激。

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>

我的Gridview 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");
    }
}

}

將此用於時間選擇器對話框, https://github.com/wdullaer/MaterialDateTimePicker

單擊您的recyclerview項目時,獲取該項目的位置,然后顯示時間選擇器對話框。 並將值設置為所選項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM