时间选择器00:00:00 AM / PM格式,在android中有秒

时间:2012-03-23 11:33:27

标签: android

在我的Time Tracking android项目中,我希望TimePicker具有秒字段。谷歌搜索后,我没有找到任何解决方案&最后我为android定制了TimePicker,它在00:00:00 AM / PM格式中给出了时间值。我认为这段代码对Android开发人员有帮助,所以我将这段代码上传到Stack。

main.xml中

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button 
            android:text="Time"
            android:id="@+id/time_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            />
        <TextView               
            android:layout_centerInParent="true"
            android:id="@+id/time_txt"                 
            android:textStyle="bold"
            android:textSize="25dp"
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            />            
    </RelativeLayout>

customdialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="4dp"
        android:background="#ffffff"
         >            
        <RelativeLayout 
            android:id="@+id/first"
            android:layout_margin="2dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            >
            <ImageView 
                android:id="@+id/clock"
                android:layout_margin="2dp"
                android:src="@drawable/coggl_logo"
                android:layout_width="40dp"
                android:layout_height="40dp"
            />
            <TextView 
                android:id="@+id/actualtime"
                android:layout_toRightOf="@+id/clock"
                android:textColor="#ffffff"
                android:text="00:00:00 AM"
                android:textSize="25dp"
                android:textStyle="bold"
                android:layout_margin="5dp"       
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"    
            />           
        </RelativeLayout>
        <RelativeLayout 
            android:id="@+id/second"
            android:layout_below="@+id/first"
            android:layout_margin="2dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:paddingBottom="15dp"
        >
        <Button 
            android:id="@+id/plus1"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="15dp"
            android:text="+"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#000000"
            android:layout_width="50dp"
            android:layout_height="40dp"
        />
        <Button 
            android:textSize="20dp"
            android:textStyle="bold"
            android:id="@+id/plus2"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@+id/plus1"
            android:text="+"
            android:textColor="#000000"
            android:layout_width="50dp"
            android:layout_height="40dp"
        />
        <Button 
        android:textSize="20dp"
            android:textStyle="bold"
              android:id="@+id/plus3"
              android:layout_marginLeft="12dp"
              android:layout_marginRight="12dp"
              android:layout_marginTop="15dp"
              android:layout_toRightOf="@+id/plus2"


            android:text="+"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
              android:id="@+id/hours"
              android:layout_marginTop="2dp"
              android:layout_marginLeft="12dp"
              android:layout_marginRight="12dp"
              android:layout_below="@+id/plus1"
           android:text="00"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
               android:layout_toRightOf="@+id/hours"
              android:id="@+id/minutes"
              android:layout_marginTop="2dp"
              android:layout_marginLeft="12dp"
              android:layout_marginRight="12dp"
              android:layout_below="@+id/plus2"
            android:text="00"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
               android:layout_toRightOf="@+id/minutes"
              android:id="@+id/seconds"
              android:layout_marginTop="2dp"
              android:layout_marginLeft="12dp"
              android:layout_below="@+id/plus3"
            android:text="00"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
               android:layout_toRightOf="@+id/seconds"
              android:id="@+id/am"
              android:layout_marginTop="2dp"
              android:layout_marginLeft="12dp"
              android:layout_below="@+id/plus3"
           android:text="AM"
            android:textColor="#000000"
               android:layout_width="60dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
               android:layout_toRightOf="@+id/seconds"
              android:id="@+id/pm"
              android:layout_marginTop="2dp"
              android:layout_marginLeft="12dp"
              android:layout_below="@+id/plus3"
               android:text="PM"
            android:textColor="#000000"
               android:layout_width="60dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
              android:id="@+id/minus1"
              android:layout_below="@+id/hours"
              android:layout_marginLeft="12dp"
              android:layout_marginRight="12dp"
             android:layout_marginTop="2dp"
            android:text="-"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
               android:layout_toRightOf="@+id/minus1"
              android:id="@+id/minus2"
              android:layout_below="@+id/minutes"
              android:layout_marginLeft="12dp"
              android:layout_marginRight="12dp"
             android:layout_marginTop="2dp"
            android:text="-"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
           <Button android:textSize="20dp"
            android:textStyle="bold"
               android:layout_toRightOf="@+id/minus2"
              android:id="@+id/minus3"
              android:layout_below="@+id/seconds"
              android:layout_marginLeft="12dp"
              android:layout_marginRight="12dp"
             android:layout_marginTop="2dp"
            android:text="-"
            android:textColor="#000000"
               android:layout_width="50dp"
        android:layout_height="40dp"
             />
        </RelativeLayout>
        <RelativeLayout 
        android:id="@+id/third"
        android:layout_below="@+id/second"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:paddingBottom="5dp"
        >
        <Button android:textSize="20dp"
            android:textStyle="bold"
            android:id="@+id/set"
            android:layout_margin="10dp"
             android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:text="Set"
        android:textColor="#000000"
            />  
            <Button android:textSize="20dp"
            android:textStyle="bold"
                 android:id="@+id/cancel"
                android:layout_toRightOf="@+id/set"
            android:layout_margin="10dp"
             android:layout_width="135dp"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:textColor="#000000"
            />
        </RelativeLayout>
    </RelativeLayout>

CustomTimePickerDialogActivity.Java

        public class CustomTimePickerDialogActivity extends Activity {
            TextView tv;
            Button time_btn;
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.main);
                tv=(TextView)findViewById(R.id.time_txt);
                time_btn=(Button)findViewById(R.id.time_btn);
                time_btn.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         showTimePickerDialog();
                    }
                });
            }
            public void showTimePickerDialog()
            {
                final Dialog dialog=new Dialog(CustomTimePickerDialogActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.customdialog);
                final TextView actualtime=(TextView)dialog.findViewById(R.id.actualtime);
                final Button hourplus=(Button)dialog.findViewById(R.id.plus1);
                Button minplus=(Button)dialog.findViewById(R.id.plus2);
                Button secplus=(Button)dialog.findViewById(R.id.plus3);
                Button hourminus=(Button)dialog.findViewById(R.id.minus1);
                Button minminus=(Button)dialog.findViewById(R.id.minus2);
                Button secminus=(Button)dialog.findViewById(R.id.minus3);
                final Button am=(Button)dialog.findViewById(R.id.am);
                final Button pm=(Button)dialog.findViewById(R.id.pm);
                final Button hourvalue=(Button)dialog.findViewById(R.id.hours);
                final Button minvalue=(Button)dialog.findViewById(R.id.minutes);
                final Button secvalue=(Button)dialog.findViewById(R.id.seconds);
                Button set=(Button)dialog.findViewById(R.id.set);
                Button cancel=(Button)dialog.findViewById(R.id.cancel);
                dialog.show();
                 Calendar calendar = new GregorianCalendar();
                    String am_pm;
                      int hour = calendar.get(Calendar.HOUR);
                      int minute = calendar.get(Calendar.MINUTE);
                      int seconds = calendar.get(Calendar.SECOND);
                      String curhour=String.valueOf(hour);
                      String curmin=String.valueOf(minute);
                      String cursec=String.valueOf(seconds);
                      if(curhour.length()== 1)
                      {
                          curhour="0"+curhour; 
                      }
                      if(curmin.length()== 1)
                      {
                          curmin="0"+curmin; 
                      }
                      if(cursec.length()== 1)
                      {
                          cursec="0"+cursec; 
                      }
                      if(calendar.get(Calendar.AM_PM) == 0)
                      am_pm = "AM";
                      else
                      am_pm = "PM";


                if(am_pm.equalsIgnoreCase("AM"))
                {
                    am.setVisibility(View.VISIBLE);
                    pm.setVisibility(View.GONE);
                }
                else
                {
                    pm.setVisibility(View.VISIBLE);
                    am.setVisibility(View.GONE);
                }

                if(curhour.equalsIgnoreCase("00"))
                {
                    curhour="12";
                }
                 actualtime.setText(curhour+":"+curmin+":"+cursec+" "+am_pm);
                  hourvalue.setText(curhour);
                  minvalue.setText(curmin);
                  secvalue.setText(cursec);
                am.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        pm.setVisibility(View.VISIBLE);
                        am.setVisibility(View.GONE);
                        String tmphour=hourvalue.getText().toString();
                        String tmpmin=minvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        actualtime.setText(tmphour+":"+tmpmin+":"+tmpsec+" "+"PM");
                    }
                });
                pm.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        am.setVisibility(View.VISIBLE);
                        pm.setVisibility(View.GONE);
                        String tmphour=hourvalue.getText().toString();
                        String tmpmin=minvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        actualtime.setText(tmphour+":"+tmpmin+":"+tmpsec+" "+"AM");
                    }
                });
                cancel.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                set.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmphour=hourvalue.getText().toString();
                        String tmpmin=minvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        String tmpam_pm="";

                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)   
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(tmphour+":"+tmpmin+":"+tmpsec+" "+tmpam_pm);

                        dialog.dismiss();
                        tv.setText(tmphour+":"+tmpmin+":"+tmpsec+" "+tmpam_pm);
                    }
                });
                hourplus.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmphour=hourvalue.getText().toString();
                        int curdisplayhr=Integer.parseInt(tmphour);
                        int finaldisplayhr = 0;
                        if(curdisplayhr<12)
                        {
                            finaldisplayhr=curdisplayhr+1;
                        }
                        else if(curdisplayhr==12)
                        {
                            finaldisplayhr=1;
                        }
                        String finalhr=String.valueOf(finaldisplayhr);
                        if(finalhr.length()==1)
                        {
                            finalhr="0"+finalhr;
                        }
                        hourvalue.setText(finalhr);

                        String tmpmin=minvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        String tmpam_pm="";
                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(finalhr+":"+tmpmin+":"+tmpsec+" "+tmpam_pm);
                    }
                });
                minplus.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmpmin=minvalue.getText().toString();
                        int curdisplaymin=Integer.parseInt(tmpmin);
                        int finaldisplaymin = 0;
                        if(curdisplaymin<59)
                        {
                            finaldisplaymin=curdisplaymin+1;
                        }
                        else if(curdisplaymin==59)
                        {
                            finaldisplaymin=0;
                        }
                        String finalmin=String.valueOf(finaldisplaymin);
                        if(finalmin.length()==1)
                        {
                            finalmin="0"+finalmin;
                        }
                        minvalue.setText(finalmin);
                        String tmphour=hourvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        String tmpam_pm="";
                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(tmphour+":"+finalmin+":"+tmpsec+" "+tmpam_pm);
                    }
                });
                secplus.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmpsec=secvalue.getText().toString();
                        int curdisplaysec=Integer.parseInt(tmpsec);
                        int finaldisplaysec = 0;
                        if(curdisplaysec<59)
                        {
                            finaldisplaysec=curdisplaysec+1;
                        }
                        else if(curdisplaysec==59)
                        {
                            finaldisplaysec=0;
                        }
                        String finalsec=String.valueOf(finaldisplaysec);
                        if(finalsec.length()==1)
                        {
                            finalsec="0"+finalsec;
                        }
                        secvalue.setText(finalsec);
                        String tmphour=hourvalue.getText().toString();
                        String tmpmin=minvalue.getText().toString();
                        String tmpam_pm="";
                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(tmphour+":"+tmpmin+":"+finalsec+" "+tmpam_pm);
                    }
                });
                hourminus.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmphour=hourvalue.getText().toString();
                        int curdisplayhr=Integer.parseInt(tmphour);
                        int finaldisplayhr = 0;

                        if(curdisplayhr==1)
                        {
                            finaldisplayhr=12;
                        }
                        else if(curdisplayhr>1||curdisplayhr<13)
                        {
                            finaldisplayhr=curdisplayhr-1;
                        }
                        String finalhr=String.valueOf(finaldisplayhr);
                        if(finalhr.length()==1)
                        {
                            finalhr="0"+finalhr;
                        }
                        hourvalue.setText(finalhr);

                        String tmpmin=minvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        String tmpam_pm="";
                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(finalhr+":"+tmpmin+":"+tmpsec+" "+tmpam_pm);
                    }
                });
                minminus.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmpmin=minvalue.getText().toString();
                        int curdisplaymin=Integer.parseInt(tmpmin);
                        int finaldisplaymin = 0;
                         if(curdisplaymin==0)
                        {
                            finaldisplaymin=59;
                        }
                         else 
                            {
                                finaldisplaymin=curdisplaymin-1;
                            }

                        String finalmin=String.valueOf(finaldisplaymin);
                        if(finalmin.length()==1)
                        {
                            finalmin="0"+finalmin;
                        }
                        minvalue.setText(finalmin);
                        String tmphour=hourvalue.getText().toString();
                        String tmpsec=secvalue.getText().toString();
                        String tmpam_pm="";
                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(tmphour+":"+finalmin+":"+tmpsec+" "+tmpam_pm);
                    }
                });
                secminus.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String tmpsec=secvalue.getText().toString();
                        int curdisplaysec=Integer.parseInt(tmpsec);
                        int finaldisplaysec = 0;
                        if(curdisplaysec==0)
                        {
                            finaldisplaysec=59;
                        }
                        else 
                        {
                            finaldisplaysec=curdisplaysec-1;
                        }
                        String finalsec=String.valueOf(finaldisplaysec);
                        if(finalsec.length()==1)
                        {
                            finalsec="0"+finalsec;
                        }
                        secvalue.setText(finalsec);
                        String tmphour=hourvalue.getText().toString();
                        String tmpmin=minvalue.getText().toString();
                        String tmpam_pm="";
                        if(am.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="AM";
                        }
                        if(pm.getVisibility()== View.VISIBLE)
                        {
                            tmpam_pm="PM";
                        }
                        actualtime.setText(tmphour+":"+tmpmin+":"+finalsec+" "+tmpam_pm);
                    }
                });

            }
        }

0 个答案:

没有答案