如何创建单独的时间选择器类

时间:2011-02-28 05:47:53

标签: android

我创建了时间选择器。但是在我的项目中,我需要在EditText字段中插入四个时间选择器。当单击EditText时,它会提示thedog来设置时间。它在Activity类中创建时间选择器是如此冗长。所以我想要创建一个单独的时间选择器类。我创建了TimePickerClass,但它显示错误。 请帮我。

package com.sample.uiscreen;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;


public class TimePickClass {


    private int mHour;
    private int mMinute;
    EditText time;
    public static final int TIME_DIALOG_ID = 0;
    MyActivity ma=null;




// the callback received when the user "sets" the time in the dialog
   public  TimePickerDialog.OnTimeSetListener mTimeSetListener =    
                    new TimePickerDialog.OnTimeSetListener() {
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
           mHour = hourOfDay;
           mMinute = minute;
           time.setText(        
                    new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));}
     //pad method
       private String pad(int c) {
           if (c >= 10)
               return String.valueOf(c);
           else        return "0" + String.valueOf(c);       }    };

           protected Dialog onCreateDialog(int id) {
               switch (id) {    
               case TIME_DIALOG_ID:        
            return new TimePickerDialog(ma,
                    mTimeSetListener, mHour, mMinute, true);    }    
               return null; }





}

1 个答案:

答案 0 :(得分:0)

我的代码如下

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // capture our View elements
        settime1 = (EditText) findViewById(R.id.time1);
        // add a click listener to the EditText
        settime1.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
                showDialog(TIME_ID1);            }        });

    settime2 = (EditText) findViewById(R.id.time2);
    settime2.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) {
            showDialog(TIME_ID2);            }        });

       }

 // the callback received when the user "sets" the time in the dialogprivate 
    TimePickerDialog.OnTimeSetListener mTimeSetListener =    
        new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMinute = minute;
            updateDisplay();        }    };

            // updates the time we display in the 
            private void updateDisplay() {
                settime1.setText(        
                        new StringBuilder()
                        .append(pad(mHour))
                        .append(":")
                        .append(pad(mMinute)));}
            private static String pad(int c) {    if (c >= 10)        
                return String.valueOf(c);    
            else        return "0" + String.valueOf(c);}


                    protected Dialog onCreateDialog(int id) {
                        switch (id) {
                        case TIME_ID1:
                            return new TimePickerDialog(this,
                                    mTimeSetListener, mHour, mMinute, true); 
                        case TIME_ID2:
                            return new TimePickerDialog(this,
                                    mTimeSetListener, mHour, mMinute, true);}
                        return null;}