导航抽屉片段

时间:2016-10-25 12:31:21

标签: android android-fragments android-timepicker

我想在我的Android应用程序中单击导航抽屉的一个片段中的特定按钮时显示timepickerdialog。但以下代码显示错误。

fragment_schedule.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundDefault"
    android:gravity="center_horizontal"
    tools:context=".Fragments.ScheduleFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:textSize="30sp"
        android:text="Present Set time"
        android:padding="40dp"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="270dp"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="10dp">

        <EditText
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:hint="Set Time"
            android:id="@+id/et_time"
            android:textAlignment="center"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set Time"
            android:id="@+id/btn_set_time"
            android:layout_below="@+id/et_time"
            android:layout_centerInParent="true"
            android:background="@color/colorPrimary"
            android:textColor="@android:color/white"/>

    </RelativeLayout>
</LinearLayout>

ScheduleFragment.java

public class ScheduleFragment extends Fragment implements View.OnClickListener {
    Button btnTimePicker;
    EditText txtTime;
    private int mHour,mMinute;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.fragment_schedule,container,false);
        btnTimePicker=(Button)v.findViewById(R.id.btn_set_time);
        txtTime=(EditText)v.findViewById(R.id.et_time);
        btnTimePicker.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        if (v==btnTimePicker){
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            txtTime.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, true);

            timePickerDialog.show();
        }
    }
}

上面的代码在构造TimePickerDialog时显示错误。请告诉我什么是错的。

0 个答案:

没有答案