使用对话框片段启动时间戳

时间:2013-11-18 04:16:47

标签: java android

我正在尝试从xml

中显示的2 edittext启动时间戳

我尝试按照 android Docs

中的说明操作

我尝试了什么 ::


XML ::

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="3dp" >

                <TextView
                    android:id="@+id/lunch_from_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/image1"
                    android:layout_toRightOf="@+id/lunch_button_id"
                    android:gravity="center"
                    android:paddingLeft="30dp"
                    android:text="From"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/from_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="40dp" />

                <TextView
                    android:id="@+id/lunch_to_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/lunch_button_id"
                    android:layout_toRightOf="@+id/textView1"
                    android:gravity="center"
                    android:text="To"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <requestFocus />

                <EditText
                    android:id="@+id/to_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="40dp"
                    android:ems="10" />
            </LinearLayout>

Buffet_offerings_breakfast_menu2.java

import java.text.DateFormat;
import java.util.Calendar;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TimePicker;

import com.androidbegin.jsonparsetutorial.R;

public class Buffet_offerings_breakfast_menu2 extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

    RadioGroup radioGroup;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        final View view=inflater.inflate(R.layout.buffet_offerings_breakfast_menu2, container, false);


        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);        

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){  
          public void onCheckedChanged(RadioGroup group, int checkedId) {
             // checkedId is the RadioButton selected
                   if(checkedId == R.id.SelectDaysRG_ID)
                   {
                      view.findViewById(R.id.linearLayout_individualdays).setVisibility(View.VISIBLE);
                   }
                   else if(checkedId == R.id.WeekdaysRG_ID)
                   {
                       view.findViewById(R.id.linearLayout_individualdays).setVisibility(View.INVISIBLE);
                   }
                   else if(checkedId == R.id.WeekendsRG_ID)
                   {
                       view.findViewById(R.id.linearLayout_individualdays).setVisibility(View.INVISIBLE);
                   }

          }
        });


        return view;
    }



    @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.java

public interface TimePickerDialog {

    public interface OnTimeSetListener {

    }

}

  • 我如何完成此解决方案?
  • 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

检查一下。这是我在我的例子中使用的那个......

    private TimePickerDialog showTimePicker(){
    TimePickerDialog timePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            mCalendar.set(Calendar.MINUTE, minute);
            mToday.set(Calendar.HOUR_OF_DAY, hourOfDay);
            mToday.set(Calendar.MINUTE, minute);
            updateTimeButtonText();

        }
    }, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true);
    return timePicker;
}

答案 1 :(得分:0)

使用此类

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
}

}

public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");

}

//在OnClick事件上调用showTimeDialog。你必须传递视图ID。

//并且在onTimeSet方法中,您可以设置edittext内容。

如果它不起作用,请告诉我。