【问题标题】:How to show timepicker in Gridview Android如何在 Gridview Android 中显示时间选择器
【发布时间】:2018-04-19 11:16:56
【问题描述】:

我正在尝试从 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");
    }
}

}

【问题讨论】:

    标签: android baseadapter android-gridview timepicker


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多