我想做一个简单的TimePickerDialog
。当用户点击editText
框时,会出现时间选择器。你能帮忙吗?以下代码不会显示对话框。谢谢你的帮助。
package com.example.myproject;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.widget.DatePicker;
public class MainActivity extends Activity {
private OnTimeSetListener myTimePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(){
Calendar c=Calendar.getInstance();
int Sysday=c.get(Calendar.DAY_OF_MONTH);
int Sysmonth=c.get(Calendar.MONTH);
int Sysyear=c.get(Calendar.YEAR);
int Sysmin=c.get(Calendar.MINUTE);
int Syshour=c.get(Calendar.HOUR);
new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="mytext"
android:onClick="show"
/>
</RelativeLayout>
答案 0 :(得分:2)
更改
public void show(){
要
public void show(View v){
和
new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false);
要
(new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false)).show();
答案 1 :(得分:1)
我认为你应该明确地调用Dialog.show()
方法
TimePickerDialog dialog = new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false);
dialog.show();