我是小时和片段中的分钟值,我在其中发布了这个时间戳:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@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()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
我在片段中尝试使用此代码:
TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
Log.i("",""+hourOfDay+":"+minute);
textview.setText(""+hourOfDay+":"+minute);
}
};
但它确实设置了任何东西
你能帮我从Timepicker中获取价值吗?
修改 这是碎片......
public class EditStoreFragment extends SherlockFragment {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 9;
AwesomePagerAdapter awesomeAdapter;
TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmentview, container, false);
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) view.findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
//...
return view;
}
class AwesomePagerAdapter extends PagerAdapter {
public View v2;
DialogFragment timepicker;
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container
* The containing View in which the page will be shown.
* @param position
* The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the
* page.
*/
@Override
public Object instantiateItem(View collection, int position) {
v2 = new View(getActivity().getApplicationContext());
timepicker = new TimePickerFragment();
collection.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
final LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
//...
break;
case 1:
//...
break;
case 2:
v2 = inflater.inflate(R.layout.edit_open, (ViewGroup) null,
false);
((TextView) v2.findViewById(R.id.titel_wt)).setText("MONTAG");
textview = (TextView) v2.findViewById(R.id.textview);
textview
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timepicker.show(getActivity().getSupportFragmentManager(), "timePicker");
Log.i("","opentimepicker");
}
});
TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
Log.i("",""+hourOfDay+":"+minute);
textview.setText(""+hourOfDay+":"+minute);
textview.invalidate();
}
};
break;
case 3:
//Like in case 2 but with other textview...
break;
case 4:
//Like in case 2 but with other textview...
break;
case 5:
//Like in case 2 but with other textview...
break;
case 6:
//Like in case 2 but with other textview...
break;
case 7:
//Like in case 2 but with other textview...
break;
case 8:
//Like in case 2 but with other textview...
break;
}
((ViewPager) collection).addView(v2, 0);
return v2;
}
/**
* Remove a page for the given position. The adapter is responsible for
* removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container
* The containing View from which the page will be removed.
* @param position
* The page position to be removed.
* @param object
* The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
/**
* Called when the a change in the shown pages has been completed. At
* this point you must ensure that all of the pages have actually been
* added or removed from the container as appropriate.
*
* @param container
* The containing View which is displaying this adapter's
* page views.
*/
@Override
public void finishUpdate(View arg0) {
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
}
THX
答案 0 :(得分:2)
此代码将获得您设置的TimePicker
值,并将显示在TextView中。
tvHora = (TextView) findViewById(R.id.tvHora);
final Calendar d = Calendar.getInstance();
hour = d.get(Calendar.HOUR_OF_DAY);
minute = d.get(Calendar.MINUTE);
//set current time into textview
tvHora.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
tvHora
是我的TextView。
答案 1 :(得分:0)
我不太了解您使用mTimeSetListener
的代码。这应该有效:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
private TextView mTextView;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mTextView = (TextView) findViewById(...);
// 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 onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
Log.i("",""+hourOfDay+":"+minute);
mTextView.setText(""+hourOfDay+":"+minute);
}
}