2016-04-15 49 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); 

    } 

謝謝