1

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

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

    view.setBackgroundResource(R.color.solid_grey);

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

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

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

首先,我尝试使用单个 TextView 并检查字符串中代表文本视图中时钟的哪个索引,即被单击。但这对突出显示效果不佳。该索引也很难精确计算,因为我想不出比使用更好的主意

    (int) event.getX();

在时钟 TextView 的 OnTouchListener 内。

关于如何以最简单的方式完成此任务的任何想法?如果不是,我必须坚持使用我编写的难以维护的代码(不,我不会在这里发布)。:S

4

1 回答 1

0

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

颜色.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-stuffxml使用include-tag将它包含在您的布局中,无论您想要什么。不要更改id:视图的 s。

    <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>

枚举.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 字符串,您必须在 . 中定义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();

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

于 2013-11-01T19:42:58.507 回答