1

尝试创建响应式时间选择器,但失败了。没有崩溃和响应。

这里有什么问题?

我的日志猫:

06-30 17:16:50.650:D/GestureDetector(508):[表面触摸事件] mSweepDown False,mLRSDCnt:-1 mTouchCnt:6 mFalseSizeCnt:0

MainActivity 包含 tabhost 代码和 timepicker 代码。

主要活动:

public class MainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);


        mTabHost.addTab(
                mTabHost.newTabSpec("View").setIndicator("View",
                        getResources().getDrawable(R.drawable.ic_tab_view)),
                ViewTab.class, null);

        mTabHost.addTab(
                mTabHost.newTabSpec("Create").setIndicator("Create",
                        getResources().getDrawable(R.drawable.ic_tab_create)),
                CreateTab.class, null);




    }


    public class TimePicker extends FragmentActivity implements TimePickedListener
    {

        private Button mPickTimeButton;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_layout);



            mPickTimeButton = (Button) findViewById(R.id.time_button);
            mPickTimeButton.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    // show the time picker dialog
                    DialogFragment newFragment = new TimePickerFragment();
                    newFragment.show(getSupportFragmentManager(), "timePicker");
                }
            });
        }

        @Override
        public void onTimePicked(Calendar time)
        {
            // display the selected time in the TextView
            mPickTimeButton.setText(DateFormat.format("h:mm a", time));
        }
    }

时间选择器片段:

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

我已经使用模拟器和手机进行了测试。问题是如果我要删除 mainActivity 中的 tabhost 代码。时间选择器有效!但这就是问题所在,因为我需要两者一起工作。

4

0 回答 0