Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timob 12139 3 1 x: Android: Textfield change event behaves differently in 2.1.4 and 3.0.0 SDK #4193

Merged
merged 3 commits into from
May 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class TiUIText extends TiUIView

private boolean field;
private int maxLength = -1;
private boolean isTruncatingText = false;

protected TiEditText tv;

Expand Down Expand Up @@ -105,7 +106,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
super.onLayout(changed, left, top, right, bottom);
TiUIHelper.firePostLayoutEvent(proxy);
}

}

public TiUIText(TiViewProxy proxy, boolean field)
Expand Down Expand Up @@ -258,43 +258,51 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}

@Override
public void afterTextChanged(Editable tv)
public void afterTextChanged(Editable editable)
{

if (maxLength >= 0 && editable.length() > maxLength) {
// The input characters are more than maxLength. We need to truncate the text and reset text.
isTruncatingText = true;
String newText = editable.subSequence(0, maxLength).toString();
int cursor = tv.getSelectionStart();
if (cursor > maxLength) {
cursor = maxLength;
}
tv.setText(newText); // This method will invoke onTextChanged() and afterTextChanged().
tv.setSelection(cursor);
} else {
isTruncatingText = false;
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count)
{

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{

/** There is an Android bug regarding setting filter on EditText that impacts auto completion.
* Therefore we can't use filters to implement "maxLength" property. Instead we manipulate
* the text to achieve perfect parity with other platforms.
* Android bug url for reference: http://code.google.com/p/android/issues/detail?id=35757
/**
* There is an Android bug regarding setting filter on EditText that impacts auto completion.
* Therefore we can't use filters to implement "maxLength" property. Instead we manipulate
* the text to achieve perfect parity with other platforms.
* Android bug url for reference: http://code.google.com/p/android/issues/detail?id=35757
*/
Object prevText = proxy.getProperty(TiC.PROPERTY_VALUE);
if (maxLength >= 0 && s.length() > maxLength) {
String t = TiConvert.toString(prevText);
int cursor = tv.getSelectionStart() - 1;
tv.setText(t);
tv.setSelection(cursor);
// Can only set truncated text in afterTextChanged. Otherwise, it will crash.
return;
}
String newValue = tv.getText().toString();
if (proxy.shouldFireChange(prevText, newValue)) {
String newText = tv.getText().toString();
if (!isTruncatingText || (isTruncatingText && proxy.shouldFireChange(proxy.getProperty(TiC.PROPERTY_VALUE), newText))) {
KrollDict data = new KrollDict();
data.put("value", newValue);

proxy.setProperty(TiC.PROPERTY_VALUE, newValue);
data.put(TiC.PROPERTY_VALUE, newText);
proxy.setProperty(TiC.PROPERTY_VALUE, newText);
fireEvent(TiC.EVENT_CHANGE, data);
}
}

@Override
public void focus()
{
Expand Down