我想要的是:按钮文字现在是“08:00”。单击按钮时,它会显示一个时间戳。所选时间设置为按钮文本。 (报警应用程序)。 我已经阅读了一些关于此的教程和问题答案,但还没有找到答案。按下时钟按钮后,应用程序崩溃。如果我把onCreateView部分放在评论中就行了。它说:android.util.AndroidRuntimeException:在添加内容之前必须调用requestFeature()。
public class TimeSet extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private Button clock;
@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 View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// R.layout.my_layout - that's the layout where your textview is placed
View view = inflater.inflate(R.layout.listview_item_row, container, false);
clock = (Button)view.findViewById(R.id.ora);
// you can use your textview.
return view;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
clock.setText(new StringBuilder().append(hourOfDay).append(":").append(minute));
}
}
答案 0 :(得分:0)
来自评论
When clicks the button it shows a timepicker. The user select the time and the time chosen is showed as the buttons text (alarm app).
无需夸大布局。
http://developer.android.com/guide/topics/ui/controls/pickers.html
使用界面作为对活动的回电。
在按钮上单击显示DialogFragment
。使用界面。在Activity中实现相同的操作并将时间设置为按钮
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
public interface PickTime
{
public void returnTime(String value);
}
PickTime mCallback;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
mCallback = (PickTime) getActivity();
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
if(mCallback!=null)
{
StringBuilder sb = new StringBuilder();
sb.append(hourOfDay);
sb.append(":");
sb.append(minute);
mCallback.returnTime(sb.toString());
}
}
}
在您的活动中
public class MainActivity extends ActionBarActivity implements PickTime{
Button button;
TimePickerFragment newFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1)
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
});
}
@Override
public void returnTime(String value) {
// TODO Auto-generated method stub
button.setText(value);
}
}