Titanium JIRA Archive
Appcelerator Community (AC)

[AC-1293] Change event on TextField behaves badly on Android

GitHub Issuen/a
TypeBug
Priorityn/a
StatusResolved
ResolutionCannot Reproduce
Resolution Date2015-09-15T04:01:02.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
LabelsTCSupportTriage, TextField, change, handler
ReporterThomas Neerup
AssigneeShak Hossain
Created2014-08-12T13:19:17.000+0000
Updated2016-03-08T07:37:41.000+0000

Description

Steps to Reproduce

Create a new Classic project Pase the code from the attached file Run the App on IOS Run the App on Android device/emulator Write a combination of numbers and letters in the field

Actual Result

On IOS the change event is called and if invalid input the old value is set on the field the cursor is placed where it was before the event was called On Android the change event is called and if invalid input the old value is set on the field but now the cursor is placed infront of the text.

Expected Result

The cursor should not be repositioned On android it is virtually impossible to write a change handler to validate the contents of a TextField since the cursor is repositioned in front of the text if text is changed in the handler.

Attachments

FileDateSize
app.js2014-08-12T13:19:19.000+00001914

Comments

  1. Amimul Hossain 2014-08-16

    Hello, We have tested the issue with the code sample provided, and observed the same problem. In android for a wrong value input, the cursor moved back to the initial starting position. TESTING ENVIRONMENT CLI version 3.3.0, Titanium SDK version 3.3.0.GA Android and iOS Simulator Workaround We have tested this code that limits the user to input digits only. It's working fine for both android and iOS. CODE SAMPLE
       
       
       
       var win = Ti.UI.createWindow({
       	fullscreen : true,
       	exitOnClose : true,
       	backgroundColor : 'red'
       });
       
       var txtInput = Titanium.UI.createTextField({
           color: '#336699',
         top: 10, left: 10,
         width: 250, height: 60,
           changeFlag: true //custom property
       });
       txtInput.addEventListener('change',function(e){
           //check if first trigger of change event -> input of user
           if (e.source.changeFlag){
               //get user's input and store in temp variable
               var temp = e.source.value;
               //remove all non-digit characters in user's input
               temp = temp.replace(/[^0-9]/g,"");
               e.source.value = temp; //this will fire a second change event
               //get the new length of the value of the textfield object
               var len = e.source.value.length;
               //set the cursor position after the last character of the value of the textfield object
               e.source.setSelection(len,len);
               e.source.flag = false;
           }else{
               //the second firing of the change event do nothing
               e.source.flag = true;
           }
       });
       
       win.add(txtInput);
       win.open();
       
       
    Thanks.
  2. Mauro Parra-Miranda 2014-09-02

    Hello [~thomas.neerup@eg.dk]! Can you please take a look into the previous workaround and let me know if that solves the issue. Best Regards!
  3. Thomas Neerup 2014-10-08

    No the previous workaround does not solve the issue.. The code does not work...
  4. Thomas Neerup 2015-01-08

    Any news on this one? Or any thoughts on how to limit numeric input on a TextField on android?
  5. Amimul Hossain 2015-04-07

    I have tested this with SDK latest version. The workaround proposed works for me. I am going to mark this ticket as resolved. If you still get the error, please reopen this ticket.
  6. Thomas Neerup 2015-09-15

    OMG. How can this be closed and marked as Resolved. When the bug still exist.

JSON Source