首次点击

时间:2016-07-11 00:20:38

标签: android android-timepicker

如何在第一次点击时出现edittext上的TimePickerDialog?需要两次单击才能显示对话框(第一次单击显示键盘)。 Gradle: minSdkVersion 9 targetSdkVersion 23

布局:

<EditText
         android:id="@+id/txtTime"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1">
         <requestFocus></requestFocus>

      </EditText>

组件:

txtTime.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {


            // Process to get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog tpd = new TimePickerDialog(Pulse7DatePickerDialogActivity.this,
                new TimePickerDialog.OnTimeSetListener() {

                   @Override
                   public void onTimeSet(TimePicker view, int hourOfDay,
                                         int minute) {
                      // Display Selected time in textbox
                      txtTime.setText(hourOfDay + ":" + minute);
                   }
                }, mHour, mMinute, false);
            tpd.show();

         }
      });

1 个答案:

答案 0 :(得分:1)

在布局声明中添加android:focusableInTouchMode="false"

<EditText
     android:id="@+id/txtTime"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:focusableInTouchMode="false"
     android:layout_weight="1">         

  </EditText>