0

I would like to use two ways to change the input of my EditText field. The EditText field will be only allowing number ("12.34" format).

Normal behavior: When the user touches the EditText it should show the keyboard and just behave like a normal EditText field

Move behavior When the user touches and drags up/down I would like the EditText to be incremented/decremented.

I've searched for this type of implementation, because I expect more people have simimular implementeations, but was unable to find it.

Yesterday I tried the following code, but this doesn't work. The keyboard is still showing during draging and using the sleep is also not a very nice implementation.

Hopefully someone here can help me with a better solution.

Thanks in advance!

inputET.setOnTouchListener(otl);

private OnTouchListener otl = new OnTouchListener() {
   public boolean onTouch (View v, MotionEvent event) {

    if (v == inputET) {
        // to distinguish between touch and drag
        if (event.getX > 50) { // drag
            float oldPos = event.getX;
            thread.sleep(10);
            inputET.setText(Float.toString(Float.parseFloat(inputET.getText()) - oldPos - event.getX);
            return true;
        } else { // touch
            return false;
        }
   }
};
patrick
  • 1,282
  • 4
  • 21
  • 37

2 Answers2

1

Your code its ok, just remove the thread's sleep and add this to the activity that is showing the EditText:

android:windowSoftInputMode="stateHidden"

I would also add this to your if condition:

if (event.getAction() == MotionEvent.ACTION_MOVE && event.getX() > 50)

Tested in my device (Android > 4.0) and works, hope it helps you :)

Aballano
  • 1,037
  • 12
  • 19
1
float mLastMotionX;
@Override
public boolean onTouch(View v, MotionEvent ev) {
    // TODO Auto-generated method stub

    final int action = ev.getAction();
    final float x = ev.getX();
    switch (action) {
    case MotionEvent.ACTION_DOWN: 
    {
        mLastMotionX = x;
        break;
    }

    case MotionEvent.ACTION_MOVE: 
    {
        final int deltaY = (int) (x - mLastMotionX);

        if( Math.abs(deltaY) % 50 == 0 && deltaY != 0)
        {
            mLastMotionX  = x;
            float current = Float.valueOf(mTextView.getText().toString()) + deltaY;
            mTextView.setText("" + round(current, 2.0));
        }
        break;
    }


    }
    return true;
}


private double round( final float value, double precision  )
{
    precision = Math.pow(10, precision);
    return Math.round(value * precision) / precision;
}

You should set the initial text for your EditText some thing like 10.34.

Triode
  • 11,309
  • 2
  • 38
  • 48
  • Hi Rajesh, Thank you for your answer, but this always hides the keyboard. I would like to show it when the edittext is touched and hide it when the user triggered the move action. Do you have a solution for this too? – patrick Apr 17 '13 at 18:40
  • create a class extending the textView and do the same inside th eonTouch functiuon of the view :) – Triode Apr 17 '13 at 19:25