2016-04-15 29 views
2

无论DatePicker也不TimePicker对话框开放,当我做水龙头上RelativeLayout而调用相应的对话框我使用android:onClick="setDate"android:onClick="setTime"的DatePicker和TimePicker对话问题

TimePicker

// On clicking Time picker 
public void setTime(View v) { 

    Calendar mcurrentTime = Calendar.getInstance(); 
    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
    int minute = mcurrentTime.get(Calendar.MINUTE); 
    TimePickerDialog mTimePicker; 
    mTimePicker = new TimePickerDialog(getApplicationContext(), new TimePickerDialog.OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
      mHour = selectedHour; 
      mMinute = selectedMinute; 
      if (selectedMinute < 10) { 
       mTime = selectedHour + ":" + "0" + selectedMinute; 
      } else { 
       mTime = selectedHour + ":" + selectedMinute; 
      } 
      mTimeText.setText(mTime); 
     } 
    }, hour, minute, false); // Is 24 hour time 
} 

的DatePicker

// On clicking Date picker 
public void setDate(View v) { 

    Calendar mcurrentTime = Calendar.getInstance(); 
    final int yearr = mcurrentTime.get(Calendar.YEAR); 
    final int month = mcurrentTime.get(Calendar.MONTH); 
    final int day_of_month = mcurrentTime.get(Calendar.DAY_OF_MONTH); 

    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

      monthOfYear++; 
      mDay = dayOfMonth; 
      mMonth = monthOfYear; 
      mYear = year; 
      mDate = day_of_month + "/" + month + "/" + yearr; 
      mDateText.setText(mDate); 

     } 
    }; 
} 

我肯定坝,我有missed东西上述功能

正如你可以看到下面,我也是在点击使用这些功能:

<LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:clickable="true" 
      android:onClick="setDate" 
      android:id="@+id/date"> 

      <ImageView 
       android:id="@+id/date_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:orientation="vertical" 
       android:layout_toRightOf="@id/date_icon" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:id="@+id/date_text"      
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/date" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:id="@+id/set_date"      
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:clickable="true" 
      android:onClick="setTime" 
      android:id="@+id/time"> 

      <ImageView 
       android:id="@+id/time_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:orientation="vertical" 
       android:layout_toRightOf="@id/time_icon" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:id="@+id/time_text"      
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/time" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:id="@+id/set_time" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

     </RelativeLayout> 
+1

在tap方法中添加edittext的代码 –

+0

editext代码?? –

+0

没有意思downvote this .... – Oreo

回答

0

尝试。

<RelativeLayout 
      android:layout_width="match_parent" 
      android:clickable="true" 
      android:onClick="setTime" 
      android:layout_height="wrap_content" 
      android:id="@+id/time"> 
+0

已经这么做了... – Oreo

+0

mTimePicker.show(); –

0

嗨timepicker只是不会出现这样的,,,请尝试以下您的活动上添加这个..

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case TIME_DIALOG_ID: 
      // set time picker as current time 
      return new TimePickerDialog(this, timePickerListener, hour, minute, false); 

    } 
    return null; 
} 

使用这样的调用对话

time.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Toast.makeText(context, "Time", Toast.LENGTH_LONG).show(); 
      whichBox=1; 
      showDialog(TIME_DIALOG_ID); 
     } 
    }); 

,你甚至可以把这个外...

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() { 
    @Override 
    public void onTimeSet(TimePicker view, int hourOfDay, int minutes) { 
     // TODO Auto-generated method stub 
     hour = hourOfDay; 
     minute = minutes; 
     updateTime(hour,minute); 

    } 

谢谢