質問

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);

    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top