【问题标题】:Using timepicker to set time使用时间选择器设置时间
【发布时间】:2013-11-16 11:07:01
【问题描述】:

有两个编辑文本 ::

edittext timepicker 的 Onclick 应该会弹出

如何使用时间选择器在两个编辑文本中选择日期&设置日期

  • 我用谷歌搜索了 timepicker
  • 但我不知道如何在点击 edittext 时启动 timepicker 和 设定时间

XML::

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="3dp" >

                <TextView
                    android:id="@+id/lunch_from_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/image1"
                    android:layout_toRightOf="@+id/lunch_button_id"
                    android:gravity="center"
                    android:paddingLeft="30dp"
                    android:text="From"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/from_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="40dp" />

                <TextView
                    android:id="@+id/lunch_to_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/lunch_button_id"
                    android:layout_toRightOf="@+id/textView1"
                    android:gravity="center"
                    android:text="To"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <requestFocus />

                <EditText
                    android:id="@+id/to_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="40dp"
                    android:ems="10" />
            </LinearLayout>

BuffetOfferings_MainFragmentActivity.java

public class BuffetOfferings_MainFragmentActivity extends FragmentActivity{

    Button back_button;

    FragmentManager manager;
    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buffet_offerings_fragment_main_reference);

        Buffet_offerings_breakfast_menu1 breakfast_fragment=new Buffet_offerings_breakfast_menu1();
        Buffet_offerings_lunch_menu1 lunch_fragment=new Buffet_offerings_lunch_menu1();
        Buffet_offerings_dinner_menu1 dinner_fragment=new Buffet_offerings_dinner_menu1();

        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();

        transaction.add(R.id.BREAKFAST_LAYOUT_ID,breakfast_fragment, "breakfast_menu1_fragment");
        transaction.add(R.id.LUNCH_LAYOUT_ID,lunch_fragment, "lunch_menu1_fragment");
        transaction.add(R.id.DINNER_LAYOUT_ID,dinner_fragment, "dinner_menu1_fragment");

        transaction.commit();



        back_button=(Button) findViewById(R.id.TopNavigationBarRestaurantBuffetOfferingsBackButton);
        back_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();

            }
        });



    }



}

【问题讨论】:

  • 你想在哪个edittext中设置时间..??
  • 对于两个编辑文本....分别:)
  • 好的...所以你必须为它调用点击事件方法......并且刚刚声明了另一个静态最终 int TIME_DIALOG_ID_MY = 1 变量..

标签: android timepicker


【解决方案1】:

单击您的 EditText 或 Button....

mPickTimeButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // show the time picker dialog
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getSupportFragmentManager(), "timePicker");
        }
    }); // this is on onCreate() method..

@Override
public void onTimePicked(Calendar time)
{
    // display the selected time in the TextView
    mPickedTimeText.setText(DateFormat.format("h:mm a", time));
}

然后创建一个类 TimePickerFragment,该类通过 DialogFragment

扩展
  public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
  private TimePickedListener mListener;

  @Override
 public Dialog onCreateDialog(Bundle savedInstanceState)
  {
    // use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
  }

  @Override
   public void onAttach(Activity activity)
  {
    // when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
    super.onAttach(activity);
    try
    {
        mListener = (TimePickedListener) activity;
    }
    catch (ClassCastException e)
    {
        throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
    }
  }

 @Override
 public void onTimeSet(TimePicker view, int hourOfDay, int minute)
 {
    // when the time is selected, send it to the activity via its callback interface method
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);

    mListener.onTimePicked(c);
 }

  public static interface TimePickedListener
 {
    public void onTimePicked(Calendar time);
 }
}

【讨论】:

  • @smriti3 但是如果你运行你的应用程序会崩溃,因为你在 oncreate 上有调用片段。那么如何显示你当前的时间选择器视图??
  • 哎呀...是的,你说得对! ....让我在片段类中重新创建整个事物.....是的,我认为这是问题所在....感谢您的帮助...错误将答案标记为正确:)
  • @smriti3 谢谢你...是的,如果您有问题,请与房间聊天...thx..
  • @PiyushGupta developer.android.com/guide/topics/ui/controls/pickers.html。你不应该在这里使用DialogFragment
  • @Raghunandan 是的...为什么不呢??
【解决方案2】:

smriti3 我们可以做到,请试试我下面的示例代码,如果您发现任何问题,请告诉我,

 <EditText 
    android:id="@+id/add_TimePicker"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:inputType="none"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textColor="#38251f"                   
    android:focusable="false"/>    

并将此示例代码添加到 Activity 中

    Calendar mcurrentDate=Calendar.getInstance();
        int mHour=mcurrentDate.get(Calendar.HOUR_OF_DAY);
        int mMinute=mcurrentDate.get(Calendar.MINUTE);
        EditText mAddTime=(EditText)findViewById(R.id.add_TimePicker);    
        mAddTime.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub          

                        TimePickerDialog mTimePicker=new TimePickerDialog(AddNewMessage.this, new OnTimeSetListener() {                 
                            public void onTimeSet(TimePicker timepicker, int selectedhour, int selectedminute) {
                                // TODO Auto-generated method stub  

                             mAddTime.setText(selectedhour+":"+selectedminute);

                            }
                        },mHour, mMinute,true);
                        mTimePicker.setTitle("Set Time");
                        mTimePicker.show();   
    }
   });

【讨论】:

    【解决方案3】:

    要打开 Time Pcker 对话框,您必须放置一个 Button,或者您应该使用一个 EditText,这样您就可以在 Button 或 EditText 的点击事件上写...

    您可以找到示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多