Implemented time fields
This commit is contained in:
parent
9493c639a8
commit
c70b40e213
@ -3,31 +3,40 @@ package com.philliphsu.clock2.edittimer;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.widget.GridLayout;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ProgressBar;
|
||||
import android.text.InputType;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.philliphsu.clock2.BaseActivity;
|
||||
import com.philliphsu.clock2.R;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.OnClick;
|
||||
import butterknife.OnLongClick;
|
||||
import butterknife.OnTouch;
|
||||
|
||||
public class EditTimerActivity extends BaseActivity {
|
||||
private static final int FIELD_LENGTH = 2;
|
||||
|
||||
private final int[] mInput = new int[6];
|
||||
private int mCursorAt;
|
||||
|
||||
@Bind(R.id.appbar) ViewGroup mAppBar;
|
||||
@Bind(R.id.label) TextView mLabel;
|
||||
@Bind(R.id.duration) TextView mDuration; // TODO: Change to something suitable for input fields
|
||||
@Bind(R.id.add_one_minute) ImageButton mAddOneMinute;
|
||||
@Bind(R.id.hour) EditText mHour;
|
||||
@Bind(R.id.minute) EditText mMinute;
|
||||
@Bind(R.id.second) EditText mSecond;
|
||||
@Bind(R.id.hour_label) TextView mHourLabel;
|
||||
@Bind(R.id.minute_label) TextView mMinuteLabel;
|
||||
@Bind(R.id.second_label) TextView mSecondLabel;
|
||||
@Bind(R.id.focus_grabber) View mFocusGrabber;
|
||||
@Bind(R.id.fab) FloatingActionButton mFab;
|
||||
@Bind(R.id.stop) ImageButton mStop;
|
||||
// TODO: Consider making an abstract EditItemActivity.
|
||||
// Define these buttons in a layout file that subclasses can include
|
||||
// into their own activity layouts. Bind OnClick listeners to abstract
|
||||
// protected methods deleteItem() and saveItem().
|
||||
@Bind(R.id.delete) Button mDelete;
|
||||
@Bind(R.id.save) Button mSave;
|
||||
@Bind(R.id.numpad) GridLayout mNumpad; // TODO: Actual numpad type
|
||||
@Bind(R.id.progress_bar) ProgressBar mProgressBar;
|
||||
// Intentionally not using a (subclass of) GridLayoutNumpad, because
|
||||
// it is expedient to refrain from adapting it for timers.
|
||||
@Bind(R.id.numpad) GridLayout mNumpad;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -41,6 +50,91 @@ public class EditTimerActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
protected int menuResId() {
|
||||
// TODO: Define a menu res with a save item
|
||||
return 0;
|
||||
}
|
||||
|
||||
@OnClick({ R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
|
||||
R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine })
|
||||
void onClick(TextView view) {
|
||||
if (mFocusGrabber.isFocused())
|
||||
return;
|
||||
EditText field = getFocusedField();
|
||||
int at = field.getSelectionStart();
|
||||
field.getText().replace(at, at + 1, view.getText());
|
||||
field.setSelection(at + 1);
|
||||
if (field.getSelectionStart() == FIELD_LENGTH) {
|
||||
// At the end of the current field, so try to focus to the next field.
|
||||
// The search will return null if no view can be focused next.
|
||||
View next = field.focusSearch(View.FOCUS_RIGHT);
|
||||
if (next != null) {
|
||||
next.requestFocus();
|
||||
if (next instanceof EditText) {
|
||||
// Should always start off at the beginning of the field
|
||||
((EditText) next).setSelection(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OnTouch({ R.id.hour, R.id.minute, R.id.second })
|
||||
boolean captureTouchEvent(EditText field, MotionEvent event) {
|
||||
int inType = field.getInputType(); // backup the input type
|
||||
field.setInputType(InputType.TYPE_NULL); // disable soft input
|
||||
boolean result = field.onTouchEvent(event); // call native handler
|
||||
field.setInputType(inType); // restore input type (to show cursor)
|
||||
return result;
|
||||
}
|
||||
|
||||
@OnClick(R.id.backspace)
|
||||
void backspace() {
|
||||
if (mFocusGrabber.isFocused()) {
|
||||
mAppBar.focusSearch(mFocusGrabber, View.FOCUS_LEFT).requestFocus();
|
||||
}
|
||||
EditText field = getFocusedField();
|
||||
if (field == null)
|
||||
return;
|
||||
int at = field.getSelectionStart();
|
||||
if (at == 0) {
|
||||
// At the beginning of current field, so move focus
|
||||
// to the preceding field
|
||||
View prev = field.focusSearch(View.FOCUS_LEFT);
|
||||
if (null == prev) {
|
||||
// Reached the beginning of the hours field
|
||||
return;
|
||||
}
|
||||
if (prev.requestFocus()) {
|
||||
if (prev instanceof EditText) {
|
||||
// Always move the cursor to the end when moving focus back
|
||||
((EditText) prev).setSelection(FIELD_LENGTH);
|
||||
}
|
||||
// Recursively backspace on the newly focused field
|
||||
backspace();
|
||||
}
|
||||
} else {
|
||||
field.getText().replace(at - 1, at, "0");
|
||||
field.setSelection(at - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@OnLongClick(R.id.backspace)
|
||||
boolean clear() {
|
||||
mHour.setText("00");
|
||||
mMinute.setText("00");
|
||||
mSecond.setText("00");
|
||||
mHour.requestFocus(); // TOneverDO: call after setSelection(0), or else the cursor returns to the end of the text
|
||||
mHour.setSelection(0); // always move the cursor WHILE the field is focused, NEVER focus after!
|
||||
mMinute.setSelection(0);
|
||||
mSecond.setSelection(0);
|
||||
return true;
|
||||
}
|
||||
|
||||
@OnClick(R.id.label)
|
||||
void openEditLabelDialog() {
|
||||
// TODO: Show the edit label alert dialog.
|
||||
}
|
||||
|
||||
public final EditText getFocusedField() {
|
||||
return (EditText) mAppBar.findFocus();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,153 +1,140 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
<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"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
tools:context="com.philliphsu.clock2.edittimer.EditTimerActivity">
|
||||
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<!-- If you add elevation, also add to the ProgressBar. -->
|
||||
<RelativeLayout
|
||||
android:id="@+id/appbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@color/colorPrimary"
|
||||
android:theme="@style/AppTheme.AppBarOverlay">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||
android:layout_alignParentTop="true"
|
||||
app:contentInsetStart="72dp">
|
||||
|
||||
<!-- Set inputType to text to get single-line input -->
|
||||
<TextView
|
||||
android:id="@+id/label"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?selectableItemBackground"
|
||||
style="@style/TextAppearance.AppCompat.Title"
|
||||
android:hint="Add label"
|
||||
android:gravity="center_vertical"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"/>
|
||||
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/duration"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="99h 99m 99s"
|
||||
android:textSize="45sp"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:layout_marginStart="72dp"
|
||||
style="@style/TextAppearance.AppCompat"/>
|
||||
|
||||
<Space
|
||||
android:id="@+id/space"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="8dp"
|
||||
android:layout_below="@id/duration"/>
|
||||
|
||||
<!-- Keep same dimens as FAB for proper alignment -->
|
||||
<ImageButton
|
||||
android:id="@+id/add_one_minute"
|
||||
android:layout_width="56dp"
|
||||
android:layout_height="56dp"
|
||||
android:src="@drawable/ic_half_day_1_black_24dp"
|
||||
android:layout_alignParentStart="true"
|
||||
android:background="?selectableItemBackground"
|
||||
android:layout_below="@id/space"
|
||||
android:layout_marginStart="16dp"/>
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/ic_half_day_1_black_24dp"
|
||||
android:layout_below="@id/space"
|
||||
android:layout_centerHorizontal="true"/>
|
||||
|
||||
<!-- Keep same dimens as FAB for proper alignment -->
|
||||
<ImageButton
|
||||
android:id="@+id/stop"
|
||||
android:layout_width="56dp"
|
||||
android:layout_height="56dp"
|
||||
android:src="@drawable/ic_half_day_1_black_24dp"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:background="?selectableItemBackground"
|
||||
android:layout_below="@id/space"
|
||||
android:layout_marginEnd="16dp"/>
|
||||
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="8dp"
|
||||
android:layout_below="@id/stop"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<!-- Must be fixed height or vertical divider will
|
||||
stretch the entire height -->
|
||||
<LinearLayout
|
||||
android:id="@+id/footer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:orientation="horizontal"
|
||||
android:layout_alignParentBottom="true">
|
||||
|
||||
<Button
|
||||
android:id="@+id/delete"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
style="@style/Widget.AppCompat.Button.Borderless.Colored"
|
||||
android:text="@string/delete"/>
|
||||
|
||||
<View style="@style/Divider.Vertical"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/save"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
style="@style/Widget.AppCompat.Button.Borderless.Colored"
|
||||
android:text="@string/save"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- TODO: Make and use (Timer/DurationPicker)Numpad -->
|
||||
<android.support.v7.widget.GridLayout
|
||||
android:id="@+id/numpad"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:columnCount="3"
|
||||
android:layout_below="@id/appbar"
|
||||
android:layout_above="@id/footer">
|
||||
|
||||
<include layout="@layout/content_grid_layout_numpad"/>
|
||||
|
||||
</android.support.v7.widget.GridLayout>
|
||||
|
||||
<View style="@style/Divider.Horizontal"
|
||||
android:layout_above="@id/footer"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_bar"
|
||||
android:id="@+id/appbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_anchor="@id/appbar"
|
||||
app:layout_anchorGravity="bottom"
|
||||
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
|
||||
android:progress="90"/>
|
||||
android:background="@color/colorPrimary"
|
||||
android:theme="@style/AppTheme.AppBarOverlay"
|
||||
android:layout_alignParentTop="true"
|
||||
android:paddingBottom="16dp">
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||
app:contentInsetStart="72dp"
|
||||
android:layout_alignParentTop="true">
|
||||
|
||||
<!-- Set inputType to text to get single-line input -->
|
||||
<TextView
|
||||
android:id="@+id/label"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?selectableItemBackground"
|
||||
style="@style/TextAppearance.AppCompat.Title"
|
||||
android:hint="Add label"
|
||||
android:gravity="center_vertical"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"/>
|
||||
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/hour"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="00"
|
||||
android:background="@android:color/transparent"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:textSize="45sp"
|
||||
android:layout_marginStart="72dp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/hour_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="h"
|
||||
android:textSize="24sp"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:layout_toEndOf="@id/hour"
|
||||
android:layout_alignBaseline="@id/hour"
|
||||
style="@style/TextAppearance.AppCompat"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/minute"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="00"
|
||||
android:background="@android:color/transparent"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:layout_toEndOf="@id/hour_label"
|
||||
android:textSize="45sp"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/minute_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="m"
|
||||
android:textSize="24sp"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:layout_toEndOf="@id/minute"
|
||||
android:layout_alignBaseline="@id/minute"
|
||||
style="@style/TextAppearance.AppCompat"
|
||||
/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/second"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="00"
|
||||
android:background="@android:color/transparent"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:layout_toEndOf="@id/minute_label"
|
||||
android:textSize="45sp"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/second_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="s"
|
||||
android:textSize="24sp"
|
||||
android:layout_below="@id/toolbar"
|
||||
android:layout_toEndOf="@id/second"
|
||||
android:layout_alignBaseline="@id/minute"
|
||||
style="@style/TextAppearance.AppCompat"
|
||||
/>
|
||||
|
||||
<View
|
||||
android:id="@+id/focus_grabber"
|
||||
style="@style/FocusGrabber"
|
||||
android:layout_toEndOf="@id/second_label"/> <!-- Required for right focus search -->
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/ic_half_day_1_black_24dp"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"/>
|
||||
|
||||
<!-- We don't really need the overhead of the GridLayoutNumpad,
|
||||
because it would be a major hassle to adapt it for timers. -->
|
||||
<android.support.v7.widget.GridLayout
|
||||
android:id="@+id/numpad"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@id/appbar"
|
||||
android:layout_above="@id/fab"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
app:columnCount="3">
|
||||
|
||||
<include layout="@layout/content_numpad_timer"/>
|
||||
|
||||
</android.support.v7.widget.GridLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
@ -1,56 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:grid="http://schemas.android.com/apk/res-auto">
|
||||
<Button
|
||||
android:id="@+id/one"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="1"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/two"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="2"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/three"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="3"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/four"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="4"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/five"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="5"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/six"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="6"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/seven"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="7"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/eight"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="8"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/nine"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
android:text="9"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/zero"
|
||||
style="@style/GridLayoutNumpadButton"
|
||||
grid:layout_column="1"
|
||||
android:text="0"/>
|
||||
<include layout="@layout/content_grid_layout_numpad"/>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/backspace"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user