如何在Andriod 2.3.3中点击文本框弹出TimePicker?

时间:2017-07-29 10:34:02

标签: android

我是Android开发的新手,我需要一个文本框来关注或点击,时间选择器应该弹出一个文本框。请在这方面帮助我。我做了很多谷歌搜索,找不到合适的解决方案。

谢谢

2 个答案:

答案 0 :(得分:2)

执行以下操作,您的文本框将获得一个漂亮的时间选择器:

final EditText my_Time = (EditText) findViewById(R.id.my_time);

    my_Time.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);

            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(TimeActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                   my_Time.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, true);
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });

在xml中创建编辑文本

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/my_time"
            android:text="20:00"
            android:editable="false" 
            android:focusable="false"
            />

答案 1 :(得分:0)

public class TestApp extends Activity {

private TimePicker timePicker1;
private Button btnPopUp;
private int hour;
private int minute;

static final int TIME_ID = 999;

现在,在On Create方法中调用按钮单击。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(Test.layout.main);
    addListenerOnButton();

}

现在,按钮点击事件

public void addListenerOnButton(){

    btnPopUp = (Button) findViewById(Test.id.btnPopUp);

    btnPopUp.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(TIME_ID);

        }

    });

}

最后,在你的创建对话框方法上写下代码。

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_ID:

        return new TimePickerDialog(this,
                                    timePickerListener, hour, minute,false);

    }
    return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener =
        new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int selectedHour,
            int selectedMinute) {
        hour = selectedHour;
        minute = selectedMinute;

        tvDisplayTime.setText(new StringBuilder().append(pad(hour))
                .append(":").append(pad(minute)));

        timePicker1.setCurrentHour(hour);
        timePicker1.setCurrentMinute(minute);

    }
};

private static String pad(int c) {
    if (c >= 10)
       return String.valueOf(c);
    else
       return "0" + String.valueOf(c);
}

希望这有帮助。