使用按钮的Android数字式时钟textview timepicker

时间:2013-10-25 22:44:51

标签: java android custom-controls timepicker

我是新手Java程序员和新手Android应用程序开发人员......我正在尝试制作一个简单的24小时数字时钟,就像一个时间戳。在这种情况下,我不想使用标准的TimePicker小部件。应用程序也适用于Android 2.1+。

我认为我的时钟看起来像 23:59 。当用户点击最右边的时钟字段时,从0到9(放在同一片段中)的按钮应该更新最右边的字段。该领域也应该突出显示。我用

完成了这个
    view.setBackgroundResource(R.color.solid_grey);

其他字段应该以相同的方式更新,当然有一些逻辑可以避免无效值。当用户触摸另一个字段时,应从第一个触摸的字段中删除突出显示。

我解决问题的方法很糟糕: 我做的是制作五个TextView,每个数字一个,冒号一个。我已将onTouch侦听器附加到时钟中的可更改字段和按钮的onClick侦听器。然后我有一些或多或少复杂的代码,其中包含查看器和带有viewholder的标记按钮,以及什么不能使所有这些工作。

必须有更好的方法来做到这一点!你不觉得吗?

首先,我尝试使用单个TextView,然后检查字符串中哪个索引代表textview中的时钟,该索引被单击。但这突出显示效果不佳。该指数也难以精确计算,因为我无法想出比使用

更好的想法
    (int) event.getX();

在OnTouchListener中为时钟TextView。

关于如何以最简单的方式实现这一目标的任何想法?如果没有,我必须坚持难以维护的代码(我不会在这里发布)。 :S

1 个答案:

答案 0 :(得分:0)

好的,我会在这里发布自己庞大的解决方案。它可能不是很漂亮,但它正在发挥作用。随意根据自己的喜好进行修改。请记住,我是来自瑞典的新手。 :P

<强> colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>    
        <!-- The two most significant hex sets the transparency value -->
        <color name="timefield_highlight_color">#FF8F8F8F</color>
        <color name="timefield_no_highlight_color">#FF000000</color>
    </resources>

<强> timepicker_digital_24h.xml 根据自己的喜好更改@dimen-stuff。在您的布局中,使用xml include-tag将其包含在您想要的任何位置。请勿更改视图的id:

    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/layout_timepicker_digital_24h"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/vertical_margin" >

            <TextView
                android:id="@+id/textview_time_set_colon_divider"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:maxLength="1"
                android:singleLine="true"
                android:text=""
                android:textSize="@dimen/alarm_time_huge_textsize"
                tools:ignore="SpUsage" />

            <TextView
                android:id="@+id/textview_time_set_hour_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/textview_time_set_colon_divider"
                android:maxLength="1"
                android:singleLine="true"
                android:text=""
                android:textSize="@dimen/alarm_time_huge_textsize"
                tools:ignore="SpUsage" />

            <TextView
                android:id="@+id/textview_time_set_hour_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/textview_time_set_hour_right"
                android:maxLength="1"
                android:singleLine="true"
                android:text=""
                android:textSize="@dimen/alarm_time_huge_textsize"
                tools:ignore="SpUsage" />

            <TextView
                android:id="@+id/textview_time_set_minute_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/textview_time_set_colon_divider"
                android:maxLength="1"
                android:singleLine="true"
                android:text=""
                android:textSize="@dimen/alarm_time_huge_textsize"
                tools:ignore="SpUsage" />

            <TextView
                android:id="@+id/textview_time_set_minute_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/textview_time_set_minute_left"
                android:maxLength="1"
                android:singleLine="true"
                android:text=""
                android:textSize="@dimen/alarm_time_huge_textsize"
                tools:ignore="SpUsage" />
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/layout_time_buttons_row_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/horizontal_margin_small"
            android:layout_marginRight="@dimen/horizontal_margin_small" >

            <Button
                android:id="@+id/button_1_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="1"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button_2_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="2"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button_3_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="3"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout_time_buttons_row_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/horizontal_margin_small"
            android:layout_marginRight="@dimen/horizontal_margin_small" >

            <Button
                android:id="@+id/button_4_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="4"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button_5_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="5"      
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button_6_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="6"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout_time_buttons_row_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/horizontal_margin_small"
            android:layout_marginRight="@dimen/horizontal_margin_small" >

            <Button
                android:id="@+id/button_7_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="7"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button_8_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="8"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />

            <Button
                android:id="@+id/button_9_time_set"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/button_set_time_margin"
                android:layout_weight="1"
                android:text="9"
                android:textSize="@dimen/button_set_time_textsize"
                tools:ignore="HardcodedText" />
        </LinearLayout>

        <Button
            android:id="@+id/button_0_time_set"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/vertical_margin_large"
            android:layout_marginLeft="@dimen/horizontal_margin"
            android:layout_marginRight="@dimen/horizontal_margin"
            android:layout_marginTop="@dimen/button_set_time_margin"
            android:text="0"
            android:textSize="@dimen/button_set_time_textsize"
            tools:ignore="HardcodedText" />

    </merge>

<强> Enums.java

    package com.example.example.timepicker;

    public class Enums {

            public static enum TimeField {
                HOUR_LEFT, HOUR_RIGHT, MINUTE_LEFT, MINUTE_RIGHT, NONE;


            public TimeField nextReal()  {  

                TimeField fields[] = TimeField.values();
                int ordinal = this.ordinal(); // incoming field index

                switch(ordinal) {
                case 0:                                     // HOUR_LEFT
                    ordinal = 1;
                    break;
                case 1:                                     // HOUR_RIGHT
                    ordinal = 2;
                    break;
                case 2:                             // MINUTE_LEFT
                    ordinal = 3;
                    break;
                case 3:                             // MINUTE_RIGHT
                    ordinal = 0;
                    break;
                case 4:                             // NONE
                    ordinal = 0;
                }
                return fields[ordinal];
            }  
            }

    }

<强> TimePickerDigital24h.java

(此代码中有一个toast-string,您必须在strings.xml中定义。)

    package com.example.example.timepicker;

    import android.app.Activity;
    import android.content.Context;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.example.example.R;
    import com.example.example.timepicker.Enums.TimeField;

    public class TimePickerDigital24h implements OnClickListener, OnTouchListener {

            private TimeField mTimeFieldToSet = TimeField.HOUR_LEFT;
            private TextView mTextViewHourLeft, mTextViewHourRight, mTextViewMinuteLeft, mTextViewMinuteRight;
            private Context mContext;

            public TimePickerDigital24h (Context context, String hourOfDay, String minute) {

                    mContext = context;

                    mTextViewHourLeft = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_hour_left);
                    mTextViewHourLeft.setText(hourOfDay.substring(0, 1));
                    mTextViewHourRight = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_hour_right);   
                    mTextViewHourRight.setText(hourOfDay.substring(1, 2));
                    mTextViewMinuteLeft = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_minute_left);
                    mTextViewMinuteLeft.setText(minute.substring(0, 1));
                    mTextViewMinuteRight = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_minute_right);
                    mTextViewMinuteRight.setText(minute.substring(1, 2));

                    mTextViewHourLeft.setOnTouchListener(this);
                    mTextViewHourRight.setOnTouchListener(this);
                    mTextViewMinuteLeft.setOnTouchListener(this);
                    mTextViewMinuteRight.setOnTouchListener(this);

                    this.setTimeFieldHighlight(TimeField.HOUR_LEFT);

                    Button button0 = (Button) ((Activity) context).findViewById(R.id.button_0_time_set);
                    button0.setOnClickListener(this);
                    Button button1 = (Button) ((Activity) context).findViewById(R.id.button_1_time_set);
                    button1.setOnClickListener(this);
                    Button button2 = (Button) ((Activity) context).findViewById(R.id.button_2_time_set);
                    button2.setOnClickListener(this);
                    Button button3 = (Button) ((Activity) context).findViewById(R.id.button_3_time_set);
                    button3.setOnClickListener(this);
                    Button button4 = (Button) ((Activity) context).findViewById(R.id.button_4_time_set);
                    button4.setOnClickListener(this);
                    Button button5 = (Button) ((Activity) context).findViewById(R.id.button_5_time_set);
                    button5.setOnClickListener(this);
                    Button button6 = (Button) ((Activity) context).findViewById(R.id.button_6_time_set);
                    button6.setOnClickListener(this);
                    Button button7 = (Button) ((Activity) context).findViewById(R.id.button_7_time_set);
                    button7.setOnClickListener(this);
                    Button button8 = (Button) ((Activity) context).findViewById(R.id.button_8_time_set);
                    button8.setOnClickListener(this);
                    Button button9 = (Button) ((Activity) context).findViewById(R.id.button_9_time_set);
                    button9.setOnClickListener(this);
            }


            @Override
            public boolean onTouch(View view, MotionEvent event) {
                    switch(view.getId()) {
                    case R.id.textview_time_set_hour_left:
                            mTimeFieldToSet = TimeField.HOUR_LEFT;
                            this.setTimeFieldHighlight(mTimeFieldToSet);
                            return true;
                    case R.id.textview_time_set_hour_right:
                            mTimeFieldToSet = TimeField.HOUR_RIGHT;
                            this.setTimeFieldHighlight(mTimeFieldToSet);
                            return true;
                    case R.id.textview_time_set_minute_left:
                            mTimeFieldToSet = TimeField.MINUTE_LEFT;
                            this.setTimeFieldHighlight(mTimeFieldToSet);
                            return true;
                    case R.id.textview_time_set_minute_right:
                            mTimeFieldToSet = TimeField.MINUTE_RIGHT;
                            this.setTimeFieldHighlight(mTimeFieldToSet);
                            return true;
                    }
                    return false;
            }


            @Override
            public void onClick(View view) {

                    int valueToSet = 0;

                    switch(view.getId()) {
                    case R.id.button_0_time_set:
                            valueToSet = 0;
                            break;
                    case R.id.button_1_time_set:
                            valueToSet = 1;
                            break;
                    case R.id.button_2_time_set:
                            valueToSet = 2;
                            break;
                    case R.id.button_3_time_set:
                            valueToSet = 3;
                            break;
                    case R.id.button_4_time_set:
                            valueToSet = 4;
                            break;
                    case R.id.button_5_time_set:
                            valueToSet = 5;
                            break;
                    case R.id.button_6_time_set:
                            valueToSet = 6;
                            break;
                    case R.id.button_7_time_set:
                            valueToSet = 7;
                            break;
                    case R.id.button_8_time_set:
                            valueToSet = 8;
                            break;
                    case R.id.button_9_time_set:
                            valueToSet = 9;
                            break;
                    }

                    try {
                            this.setTimeField(valueToSet);
                    } catch (UnsupportedOperationException e) {
                            Toast.makeText(mContext, mContext.getString(R.string.toast_time_set_error), Toast.LENGTH_LONG).show();
                            //e.printStackTrace();
                    }

            }

            // Setter for timefields in the clock time display. Also highlights the correct field.
            private void setTimeField (int valueToSet) throws UnsupportedOperationException {
                    int hourLeft = Integer.parseInt(mTextViewHourLeft.getText().toString());
                    int hourRight = Integer.parseInt(mTextViewHourRight.getText().toString());

                    UnsupportedOperationException exception = new UnsupportedOperationException("Input value invalid for this clock field");

                    setTimeFieldHighlight(mTimeFieldToSet.nextReal());

                    switch(mTimeFieldToSet) {       
                    case HOUR_LEFT:
                            if (valueToSet <= 1) {
                                    mTextViewHourLeft.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.HOUR_RIGHT;
                                    break;
                            } else if (valueToSet <= 2 && hourRight <= 3) {
                                    mTextViewHourLeft.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.HOUR_RIGHT;
                                    break;
                            } else if (valueToSet <= 2 && hourRight >= 4) {
                                    mTextViewHourRight.setText("3");
                                    mTextViewHourLeft.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.HOUR_RIGHT;
                                    break;
                            } else {
                                    setTimeFieldHighlight(mTimeFieldToSet);
                                    throw exception;
                            }

                    case HOUR_RIGHT:
                            if (valueToSet <= 3) {
                                    mTextViewHourRight.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.MINUTE_LEFT;
                                    break;
                            } else if (valueToSet > 3 && hourLeft <= 1) {
                                    mTextViewHourRight.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.MINUTE_LEFT;
                                    break;
                            } else if (valueToSet > 3 && hourLeft >= 2) {
                                    mTextViewHourLeft.setText("1");
                                    mTextViewHourRight.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.MINUTE_LEFT;
                                    break;
                            } else {
                                    setTimeFieldHighlight(mTimeFieldToSet);
                                    throw exception;
                            }

                    case MINUTE_LEFT:
                            if (valueToSet <= 5) {
                                    mTextViewMinuteLeft.setText("" + valueToSet);
                                    mTimeFieldToSet = TimeField.MINUTE_RIGHT;
                                    break;
                            } else {
                                    setTimeFieldHighlight(mTimeFieldToSet);
                                    throw exception;
                            }

                    case MINUTE_RIGHT:
                            mTextViewMinuteRight.setText("" + valueToSet);
                            mTimeFieldToSet = TimeField.HOUR_LEFT;
                            break;

                    case NONE:
                    }

            }

            // Highlighting of the fields in the clock display
            private void setTimeFieldHighlight(TimeField field) {

                    switch(field) {
                    case HOUR_LEFT:
                            mTextViewHourLeft.setBackgroundResource(R.color.timefield_highlight_color);
                            mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            break;
                    case HOUR_RIGHT:
                            mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewHourRight.setBackgroundResource(R.color.timefield_highlight_color);
                            mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            break;
                    case MINUTE_LEFT:
                            mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_highlight_color);
                            mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            break;
                    case MINUTE_RIGHT:
                            mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteRight.setBackgroundResource(R.color.timefield_highlight_color);
                            break;
                    case NONE:
                            mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
                            mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
                    }

            }

            public String getHourOfDay() {
                    String hourOfDay = mTextViewHourLeft.getText().toString()
                                    + mTextViewHourRight.getText().toString();
                    return hourOfDay;
            }

            public String getMinute() {
                    String minute = mTextViewMinuteLeft.getText().toString()
                                    + mTextViewMinuteRight.getText().toString();
                    return minute;
            }

    }

**将此代码放入Fragment的OnActivityCreated方法中以实例化TimePickerDigital24h对象:**

    mTimePicker = new TimePickerDigital24h(getActivity(), "23", "59");

您可以通过以下方式回读用户设置的时间:

    mTimePicker.getHourOfDay();
    mTimePicker.getMinute();

我确信代码在很多方面都会更好。如果你们以更简单的方式知道如何做到这一点,请告诉我。 / TiredDude