我正在尝试制作一个简单的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>
最佳答案
更改
public void show(){
至
public void show(View v){
和
new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false);
至
(new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false)).show();