1 |
/*
|
2 |
* Copyright (C) 2008 The Android Open Source Project
|
3 |
*
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
* you may not use this file except in compliance with the License.
|
6 |
* You may obtain a copy of the License at
|
7 |
*
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
9 |
*
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
* See the License for the specific language governing permissions and
|
14 |
* limitations under the License.
|
15 |
*/
|
16 |
|
17 |
package com.example.widget;
|
18 |
|
19 |
import android.content.Context;
|
20 |
import android.util.AttributeSet;
|
21 |
import android.view.KeyEvent;
|
22 |
import android.view.MotionEvent;
|
23 |
import android.widget.ImageButton;
|
24 |
|
25 |
import com.zoffcc.applications.aagtl.R;
|
26 |
|
27 |
/**
|
28 |
* This class exists purely to cancel long click events.
|
29 |
*/
|
30 |
public class NumberPickerButton extends ImageButton
|
31 |
{
|
32 |
|
33 |
private NumberPicker mNumberPicker;
|
34 |
|
35 |
public NumberPickerButton(Context context, AttributeSet attrs, int defStyle)
|
36 |
{
|
37 |
super(context, attrs, defStyle);
|
38 |
}
|
39 |
|
40 |
public NumberPickerButton(Context context, AttributeSet attrs)
|
41 |
{
|
42 |
super(context, attrs);
|
43 |
}
|
44 |
|
45 |
public NumberPickerButton(Context context)
|
46 |
{
|
47 |
super(context);
|
48 |
}
|
49 |
|
50 |
public void setNumberPicker(NumberPicker picker)
|
51 |
{
|
52 |
mNumberPicker = picker;
|
53 |
}
|
54 |
|
55 |
@Override
|
56 |
public boolean onTouchEvent(MotionEvent event)
|
57 |
{
|
58 |
cancelLongpressIfRequired(event);
|
59 |
return super.onTouchEvent(event);
|
60 |
}
|
61 |
|
62 |
@Override
|
63 |
public boolean onTrackballEvent(MotionEvent event)
|
64 |
{
|
65 |
cancelLongpressIfRequired(event);
|
66 |
return super.onTrackballEvent(event);
|
67 |
}
|
68 |
|
69 |
@Override
|
70 |
public boolean onKeyUp(int keyCode, KeyEvent event)
|
71 |
{
|
72 |
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER))
|
73 |
{
|
74 |
cancelLongpress();
|
75 |
}
|
76 |
return super.onKeyUp(keyCode, event);
|
77 |
}
|
78 |
|
79 |
private void cancelLongpressIfRequired(MotionEvent event)
|
80 |
{
|
81 |
if ((event.getAction() == MotionEvent.ACTION_CANCEL)
|
82 |
|| (event.getAction() == MotionEvent.ACTION_UP))
|
83 |
{
|
84 |
cancelLongpress();
|
85 |
}
|
86 |
}
|
87 |
|
88 |
private void cancelLongpress()
|
89 |
{
|
90 |
if (R.id.increment == getId())
|
91 |
{
|
92 |
mNumberPicker.cancelIncrement();
|
93 |
}
|
94 |
else if (R.id.decrement == getId())
|
95 |
{
|
96 |
mNumberPicker.cancelDecrement();
|
97 |
}
|
98 |
}
|
99 |
}
|