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
This commit is contained in:
James Cook
2015-02-12 11:13:16 -08:00
parent 037d593168
commit 5c42f75896
4 changed files with 164 additions and 44 deletions

View File

@@ -1176,7 +1176,6 @@
</intent-filter> </intent-filter>
</activity> </activity>
<!--
<activity android:name=".content.TextUndoActivity" android:label="@string/activity_text_undo"> <activity android:name=".content.TextUndoActivity" android:label="@string/activity_text_undo">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -1184,7 +1183,6 @@
<category android:name="android.intent.category.EMBED" /> <category android:name="android.intent.category.EMBED" />
</intent-filter> </intent-filter>
</activity> </activity>
-->
<activity android:name=".content.ResourcesLayoutReference" <activity android:name=".content.ResourcesLayoutReference"
android:label="@string/activity_resources_layout_reference"> android:label="@string/activity_resources_layout_reference">

View File

@@ -14,21 +14,20 @@
limitations under the License. limitations under the License.
--> -->
<!-- Demonstrates saving and restoring activity state. <!-- Demonstrates undo/redo behavior on a text field. -->
See corresponding Java code com.android.sdk.app.SaveRestoreState.java. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:orientation="vertical" android:padding="4dip" <LinearLayout android:orientation="vertical" android:padding="4dip"
android:layout_width="match_parent" android:layout_height="wrap_content"> android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/msg" <TextView android:id="@+id/default_edit_msg"
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_weight="0" android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingBottom="8dip" android:paddingBottom="8dip"
android:text="@string/text_undo_msg" /> android:text="@string/text_undo_default_edit_msg" />
<EditText android:id="@+id/text" <EditText android:id="@+id/default_text"
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
@@ -41,18 +40,73 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingTop="8dip"> android:paddingTop="8dip">
<Button <Button
android:id="@+id/undo" android:id="@+id/set_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/undo" android:text="@string/text_undo_set_text"
android:layout_gravity="bottom" />
<Button
android:id="@+id/append_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_undo_append_text"
android:layout_gravity="bottom" /> android:layout_gravity="bottom" />
<Button <Button
android:id="@+id/redo" android:id="@+id/insert_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/redo" android:text="@string/text_undo_insert_text"
android:layout_gravity="bottom" /> android:layout_gravity="bottom" />
</LinearLayout> </LinearLayout>
<TextView android:id="@+id/undo_disabled_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingBottom="8dip"
android:text="@string/text_undo_undo_disabled_msg" />
<EditText android:id="@+id/undo_disabled_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:freezesText="true"
android:allowUndo="false">
</EditText>
<TextView android:id="@+id/length_limit_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingBottom="8dip"
android:text="@string/text_undo_length_limit_msg" />
<EditText android:id="@+id/length_limit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:freezesText="true">
</EditText>
<TextView android:id="@+id/credit_card_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingBottom="8dip"
android:text="@string/text_undo_credit_card_msg" />
<EditText android:id="@+id/credit_card_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:freezesText="true">
</EditText>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View File

@@ -421,11 +421,14 @@
<string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string> <string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>
<string name="styled_text_prog">Assigned programmatically:</string> <string name="styled_text_prog">Assigned programmatically:</string>
<string name="activity_text_undo">Content/Undo Manager/Text</string> <string name="activity_text_undo">Content/Undo</string>
<string name="text_undo_msg">Demonstrates simple use of UndoManager with text editing <string name="text_undo_default_edit_msg">TextView with the default Control-Z undo behavior.</string>
in a TextView.</string> <string name="text_undo_set_text">SetText</string>
<string name="undo">Undo</string> <string name="text_undo_append_text">Append</string>
<string name="redo">Redo</string> <string name="text_undo_insert_text">Insert</string>
<string name="text_undo_undo_disabled_msg">TextView with undo disabled.</string>
<string name="text_undo_length_limit_msg">TextView with a length limit InputFilter.</string>
<string name="text_undo_credit_card_msg">Credit card input field with a TextWatcher.</string>
<string name="activity_resources_layout_reference">Content/Resources/Layout Reference</string> <string name="activity_resources_layout_reference">Content/Resources/Layout Reference</string>
<string name="resources_layout_reference_description">Shows how to write layout <string name="resources_layout_reference_description">Shows how to write layout

View File

@@ -16,58 +16,123 @@
package com.example.android.apis.content; 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 com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle; 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. * Simple example of using an UndoManager for editing text in a TextView.
*/ */
public class TextUndoActivity extends Activity { 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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); setContentView(R.layout.text_undo);
/* mDefaultText = (EditText) findViewById(R.id.default_text);
((TextView)findViewById(R.id.text)).setUndoManager(mUndoManager, "text"); ((Button) findViewById(R.id.set_text)).setOnClickListener(new View.OnClickListener() {
((Button)findViewById(R.id.undo)).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
mUndoManager.undo(null, 1); mDefaultText.setText("some text");
} }
}); });
((Button)findViewById(R.id.redo)).setOnClickListener(new View.OnClickListener() { ((Button) findViewById(R.id.append_text)).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
mUndoManager.redo(null, 1); mDefaultText.append(" append");
} }
}); });
*/ ((Button) findViewById(R.id.insert_text)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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) { * A simple credit card input formatter that adds spaces every 4 characters.
super.onSaveInstanceState(outState); */
//outState.putParcelable("undo", mUndoManager.saveInstanceState()); 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';
}
} }
} }