使用时间选择器设置时间 [英] Using timepicker to set time
本文介绍了使用时间选择器设置时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有两个编辑文本 ::
编辑文本时间选择器的单击应弹出
Onclick of edittext timepicker should pop up
- 我在Google旁边搜索了时间选择器
- 但是我不知道如何在单击edittext和设定时间
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或Button....
Click on your EditText or 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
After that create a class TimePickerFragment which is extends with 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);
}
}
这篇关于使用时间选择器设置时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文