summaryrefslogtreecommitdiffstats
path: root/samples/ApiDemos/src/com/example/android
diff options
context:
space:
mode:
authorJames Cook <jamescook@google.com>2015-02-12 11:13:16 -0800
committerJames Cook <jamescook@google.com>2015-03-10 17:00:19 +0000
commit5c42f7589641c57c1cf6c487b62585583f95e702 (patch)
treef4b32bc79e375d509acfd3f026e23e5679daa26c /samples/ApiDemos/src/com/example/android
parent037d593168917f2b1115285fdc619ec8420366a3 (diff)
downloadandroid_development-5c42f7589641c57c1cf6c487b62585583f95e702.tar.gz
android_development-5c42f7589641c57c1cf6c487b62585583f95e702.tar.bz2
android_development-5c42f7589641c57c1cf6c487b62585583f95e702.zip
Re-enable the undo demo activity
The UI now shows these input fields: * A normal field with buttons for programmatic SetText, append and insert. * A field with undo disabled. * A field with a length limit (via InputFilter). * A field with a credit card input formatter (via TextWatcher) Bug: 19332904 Change-Id: I41df56c84dd5232918e7ee5e6ed5874b2023cc78
Diffstat (limited to 'samples/ApiDemos/src/com/example/android')
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/content/TextUndoActivity.java121
1 files changed, 93 insertions, 28 deletions
diff --git a/samples/ApiDemos/src/com/example/android/apis/content/TextUndoActivity.java b/samples/ApiDemos/src/com/example/android/apis/content/TextUndoActivity.java
index 6b9dc8e62..d782d312d 100644
--- a/samples/ApiDemos/src/com/example/android/apis/content/TextUndoActivity.java
+++ b/samples/ApiDemos/src/com/example/android/apis/content/TextUndoActivity.java
@@ -16,58 +16,123 @@
package com.example.android.apis.content;
-import android.app.Activity;
-//import android.content.UndoManager;
-import android.os.Parcelable;
-import android.view.View;
-import android.widget.Button;
import com.example.android.apis.R;
+import android.app.Activity;
import android.os.Bundle;
-import android.widget.TextView;
+import android.text.Editable;
+import android.text.InputFilter;
+import android.text.Spanned;
+import android.text.TextWatcher;
+import android.text.method.DigitsKeyListener;
+import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
/**
* Simple example of using an UndoManager for editing text in a TextView.
*/
public class TextUndoActivity extends Activity {
- //UndoManager mUndoManager;
+ // Characters allowed as input in the credit card field.
+ private static final String CREDIT_CARD_CHARS = "0123456789 ";
+
+ EditText mDefaultText;
+ EditText mLengthLimitText;
+ EditText mCreditCardText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- /*
- mUndoManager = new UndoManager();
- if (savedInstanceState != null) {
- Parcelable p = savedInstanceState.getParcelable("undo");
- if (p != null) {
- mUndoManager.restoreInstanceState(p);
- }
- }
- */
-
setContentView(R.layout.text_undo);
- /*
- ((TextView)findViewById(R.id.text)).setUndoManager(mUndoManager, "text");
- ((Button)findViewById(R.id.undo)).setOnClickListener(new View.OnClickListener() {
+ mDefaultText = (EditText) findViewById(R.id.default_text);
+ ((Button) findViewById(R.id.set_text)).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ mDefaultText.setText("some text");
+ }
+ });
+ ((Button) findViewById(R.id.append_text)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- mUndoManager.undo(null, 1);
+ mDefaultText.append(" append");
}
});
- ((Button)findViewById(R.id.redo)).setOnClickListener(new View.OnClickListener() {
+ ((Button) findViewById(R.id.insert_text)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- mUndoManager.redo(null, 1);
+ Editable editable = mDefaultText.getText();
+ editable.insert(0, "insert ");
}
});
- */
+
+ mLengthLimitText = (EditText) findViewById(R.id.length_limit_text);
+ mLengthLimitText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) });
+
+ mCreditCardText = (EditText) findViewById(R.id.credit_card_text);
+ mCreditCardText.setKeyListener(DigitsKeyListener.getInstance(CREDIT_CARD_CHARS));
+ mCreditCardText.addTextChangedListener(new CreditCardTextWatcher());
}
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- //outState.putParcelable("undo", mUndoManager.saveInstanceState());
+ /**
+ * A simple credit card input formatter that adds spaces every 4 characters.
+ */
+ private static class CreditCardTextWatcher implements TextWatcher {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+ }
+
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
+ }
+
+ @Override
+ public void afterTextChanged(Editable s) {
+ String original = s.toString();
+ String formatted = addSpaces(getNumbers(original));
+ // This is an ugly way to avoid infinite recursion, but it's common in app code.
+ if (!formatted.equals(original)) {
+ s.replace(0, s.length(), formatted);
+ }
+ }
+
+ /**
+ * @return Returns a string with a space added every 4 characters.
+ */
+ private static String addSpaces(CharSequence str) {
+ StringBuilder builder = new StringBuilder();
+ int len = str.length();
+ for (int i = 0; i < len; i += 4) {
+ if (i + 4 < len) {
+ builder.append(str.subSequence(i, i + 4));
+ builder.append(' ');
+ } else {
+ // Don't put a space after the end.
+ builder.append(str.subSequence(i, len));
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * @return Returns a string containing only the digits from a character sequence.
+ */
+ private static String getNumbers(CharSequence cc) {
+ StringBuilder sb = new StringBuilder(16);
+ for (int i = 0, count = cc.length(); i < count; ++i) {
+ char c = cc.charAt(i);
+ if (isNumber(c)) {
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+ private static boolean isNumber(char c) {
+ return c >= '0' && c <= '9';
+ }
+
}
}