Android的,通过DialogFragment达到按钮? [英] Android, reach Button via DialogFragment?
问题描述
我想要什么:按钮文本是08:00现在。当点击按钮,它显示了一个timepicker。选定的时间被设置为按钮的文本。 (报警的应用程序)。
我读一些有关此教程和问题,答案,但没有找到解答。之后,我preSS时钟按钮的应用程序崩溃。如果我把onCreateView参与评论它的作品。它说:android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前被称为
公共类TimeSet扩展DialogFragment实现TimePickerDialog.OnTimeSetListener { 私人按钮时钟; @覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){
//使用当前时间作为选择器的默认值
最后的日历C = Calendar.getInstance();
INT小时= c.get(Calendar.HOUR_OF_DAY);
INT分钟= c.get(Calendar.MINUTE); //创建TimePickerDialog的新实例,并将其返回
返回新TimePickerDialog(getActivity(),这个,小时,分钟,
DateFormat.is24HourFormat(getActivity()));
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ // R.layout.my_layout - 这就是你的TextView放在布局
查看查看= inflater.inflate(R.layout.listview_item_row,集装箱,FALSE);
时钟=(按钮)view.findViewById(R.id.ora);
//你可以使用你的TextView。
返回视图。
} 公共无效onTimeSet(TimePicker观点,诠释HOUROFDAY,分整型){
//做一些与用户所选择的时间
clock.setText(新的StringBuilder()追加(HOUROFDAY).append(:。)追加(分钟));
}}
从注释
当点击它显示了一个timepicker按钮。用户选择时间和选择的时间表现为按钮文本(警报应用程序)。
有不需要膨胀的布局。
http://developer.android.com/guide/topics/ UI /控制/ pickers.html
使用一个接口作为回调到活动中。
在按钮单击显示的 DialogFragment
。使用界面。实现了相同的活动,时间设置按钮
公共类TimePickerFragment扩展DialogFragment
实现TimePickerDialog.OnTimeSetListener {
公共接口PickTime
{
公共无效返回时间(字符串值); } PickTime mCallback;
@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){
//使用当前时间作为选择器的默认值
mCallback =(PickTime)getActivity();
最后的日历C = Calendar.getInstance();
INT小时= c.get(Calendar.HOUR_OF_DAY);
INT分钟= c.get(Calendar.MINUTE);//创建TimePickerDialog的新实例,并将其返回
返回新TimePickerDialog(getActivity(),这个,小时,分钟,
DateFormat.is24HourFormat(getActivity()));
}公共无效onTimeSet(TimePicker观点,诠释HOUROFDAY,分整型){
//做一些与用户所选择的时间 如果(mCallback!= NULL)
{
StringBuilder的SB =新的StringBuilder();
sb.append(HOUROFDAY);
sb.append(:);
sb.append(分钟);
mCallback.returnTime(sb.toString());
}
}
}
在你的活动
公共类MainActivity扩展ActionBarActivity实现PickTime { Button按钮;
TimePickerFragment newFragment; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.button1)
button.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
newFragment =新TimePickerFragment();
newFragment.show(getSupportFragmentManager(),timePicker); }
});
}
@覆盖
公共无效返回时间(字符串值){
// TODO自动生成方法存根
button.setText(值); }}
What i want: the buttons text is "08:00" now. When click the button it shows a timepicker. The selected time is set to the buttons text. (Alarm app). i have read some tutorials and questions-answers about this, but haven't find the answers yet. After i press the clock button the app crashes. If i put the onCreateView part in comment it works. it says: android.util.AndroidRuntimeException: requestFeature() must be called before adding content.
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));
}
}
From the comment
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).
There is no need to inflate a layout.
http://developer.android.com/guide/topics/ui/controls/pickers.html
Use a Interface as a call back to the activity.
On Button click show the DialogFragment
. Use a interface. Implement the same in Activity and set the time to button
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());
}
}
}
In your Activity
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);
}
}
这篇关于Android的,通过DialogFragment达到按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!