1 /*
2  * Copyright (C) 2013 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.android.apis.content;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.InputFilter;
25 import android.text.Spanned;
26 import android.text.TextWatcher;
27 import android.text.method.DigitsKeyListener;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.Button;
31 import android.widget.EditText;
32 
33 /**
34  * Simple example of using an UndoManager for editing text in a TextView.
35  */
36 public class TextUndoActivity extends Activity {
37     // Characters allowed as input in the credit card field.
38     private static final String CREDIT_CARD_CHARS = "0123456789 ";
39 
40     EditText mDefaultText;
41     EditText mLengthLimitText;
42     EditText mCreditCardText;
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         setContentView(R.layout.text_undo);
49 
50         mDefaultText = (EditText) findViewById(R.id.default_text);
51         ((Button) findViewById(R.id.set_text)).setOnClickListener(new View.OnClickListener() {
52             @Override
53             public void onClick(View v) {
54                 mDefaultText.setText("some text");
55             }
56         });
57         ((Button) findViewById(R.id.append_text)).setOnClickListener(new View.OnClickListener() {
58             @Override
59             public void onClick(View v) {
60                 mDefaultText.append(" append");
61             }
62         });
63         ((Button) findViewById(R.id.insert_text)).setOnClickListener(new View.OnClickListener() {
64             @Override
65             public void onClick(View v) {
66                 Editable editable = mDefaultText.getText();
67                 editable.insert(0, "insert ");
68             }
69         });
70 
71         mLengthLimitText = (EditText) findViewById(R.id.length_limit_text);
72         mLengthLimitText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) });
73 
74         mCreditCardText = (EditText) findViewById(R.id.credit_card_text);
75         mCreditCardText.setKeyListener(DigitsKeyListener.getInstance(CREDIT_CARD_CHARS));
76         mCreditCardText.addTextChangedListener(new CreditCardTextWatcher());
77      }
78 
79     /**
80      * A simple credit card input formatter that adds spaces every 4 characters.
81      */
82     private static class CreditCardTextWatcher implements TextWatcher {
83         @Override
beforeTextChanged(CharSequence s, int start, int count, int after)84         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
85         }
86 
87         @Override
onTextChanged(CharSequence s, int start, int before, int count)88         public void onTextChanged(CharSequence s, int start, int before, int count) {
89         }
90 
91         @Override
afterTextChanged(Editable s)92         public void afterTextChanged(Editable s) {
93             String original = s.toString();
94             String formatted = addSpaces(getNumbers(original));
95             // This is an ugly way to avoid infinite recursion, but it's common in app code.
96             if (!formatted.equals(original)) {
97                 s.replace(0, s.length(), formatted);
98             }
99         }
100 
101         /**
102          * @return Returns a string with a space added every 4 characters.
103          */
addSpaces(CharSequence str)104         private static String addSpaces(CharSequence str) {
105             StringBuilder builder = new StringBuilder();
106             int len = str.length();
107             for (int i = 0; i < len; i += 4) {
108                 if (i + 4 < len) {
109                     builder.append(str.subSequence(i, i + 4));
110                     builder.append(' ');
111                 } else {
112                     // Don't put a space after the end.
113                     builder.append(str.subSequence(i, len));
114                 }
115             }
116             return builder.toString();
117         }
118 
119         /**
120          * @return Returns a string containing only the digits from a character sequence.
121          */
getNumbers(CharSequence cc)122         private static String getNumbers(CharSequence cc) {
123             StringBuilder sb = new StringBuilder(16);
124             for (int i = 0, count = cc.length(); i < count; ++i) {
125                 char c = cc.charAt(i);
126                 if (isNumber(c)) {
127                     sb.append(c);
128                 }
129             }
130             return sb.toString();
131         }
132 
isNumber(char c)133         private static boolean isNumber(char c) {
134             return c >= '0' && c <= '9';
135         }
136 
137     }
138 }
139