最近编辑于2018年4月30日
比较简单,先上源码
public class TextUndoActivity extends Activity {
// 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);
setContentView(R.layout.text_undo);
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) {
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());
}
/**
* 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';
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Demonstrates undo/redo behavior on a text field. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:orientation="vertical" android:padding="4dip"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/default_edit_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_default_edit_msg" />
<EditText android:id="@+id/default_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
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="8dip">
<Button
android:id="@+id/set_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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" />
<Button
android:id="@+id/insert_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_undo_insert_text"
android:layout_gravity="bottom" />
</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>
</ScrollView>
主要就讲了三个操作:
1、点击按钮EditText插入文字的方式:
i、setText();
ii、append();
iii、getText().insert(where,text);where:起始位置;text:文本。
2、setFilters();传入参数是InputFilter数组,InputFilter有两种,一种是AllCaps,一种是LengthFilter。
3、设置监听,第一种监听是setKeyListener(DigitsKeyListener.getInstance(CREDIT_CARD_CHARS)),这个EditText中只能输入CREDIT_CARD_CHARS这个String中的字符;第二种监听是addTextChangedListener(new TextWatcher()),是对每输入一个字符时内容的监听,API Demos中在该监听中进行了每四个字符空一格(且只能输入ASCII码值在“0”到“9”之间的字符)。