2016-04-15 13 views
2

Weder DatePicker noch TimePicker Dialog eröffnen, wenn ich auf RelativeLayout tippen Sie während entsprechende Dialoge nenne ich android:onClick="setDate" und android:onClick="setTime" bin mitDatepicker und Timepicker Dialog Ausgabe

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); 

     } 
    }; 
} 

Ich bin dam sicher, dass ich missed etwas in über Funktionen

Wie Sie unten sehen können, habe ich diese Funktionen auch in auf Click bin mit:

<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

code von editiertext auf tippen methode –

+0

editext code ?? –

+0

nein, um dies zu downvote .... – Oreo

Antwort

0

es versuchen.

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

bereits dabei ... – Oreo

+0

mTimePicker.show(); –

0

Hallo Timepicker einfach gewohnt so zeigen ,,, .. folgendes fügen Sie diese auf Ihre Aktivität versuchen Sie es

@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; 
} 

Verwendung so etwas wie dieses

der Dialog aufrufen
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); 
     } 
    }); 

Sie auch diese außerhalb setzen können ...

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); 

    } 

Vielen Dank

Verwandte Themen