在Android活动中,我首先恢复EditText中的文本,然后向其添加TextWatcher.
private static int WC = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TextWatcherTest", "onCreate:\t" +CLASS_NAME);
setContentView(R.layout.main);
EditText et = (EditText)findViewById(R.id.editText);
Log.e("TextWatcherTest", "Set text xyz");
et.setText("xyz");
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) {
Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
}
});
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行活动时,即使在设置文本后添加了Watcher本身,也会调用afterTextChanged方法.所以日志输出是这样的
onCreate: LifecycleMain Set text xyz // screen rotation onCreate: LifecycleMain Set text …
所以我一直在尝试实现Android的TextWatcher并遇到一些问题,TextChangedListener被多次调用或进入无限循环,因为我想将EditText小部件中的文本转换为货币格式化的字符串.
我解决这个问题的方法是创建我自己的自定义TextWatcher然后在afterTextChanged事件中执行类似下面的操作
public class CurrencyTextWatcher implements TextWatcher {
private EditText et;
public CurrencyTextWatcher(EditText editText) {
et = editText;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
et.setText(myCurrencyString);
et.addTextChangedListener(this);
}
}Run Code Online (Sandbox Code Playgroud)
所以我的问题是,有更好的方法吗?我希望有一个EditText Widget来保存编辑所在的位置以及生成的格式化字符串.
实际上还有其他任何问题,有关删除然后添加像这样的TextChangedListener?
提前致谢
我正在编写一个使用和外部连接USB条形码/ RFID扫描仪的应用程序.正在扫描的数据是"复合"数据.这是一个例子:
=+03000=W12560712345600=%2800&>0090452359
Run Code Online (Sandbox Code Playgroud)
那是来自复合数据扫描.数据中的分隔符是等号(=)或&符号(&).第一位=+03000说扫描中有三个数据部分:
=W12560712345600
=%2800
&>0090452359
Run Code Online (Sandbox Code Playgroud)
该数据可以具有从1到N的任意数量的数据部分.
在我的Android应用程序中,我有一个包含三个EditText元素的表单.我需要对这个复合扫描数据做的是使用分隔符将其分解,并将每个数据粘贴到适当的EditText字段中.
经过多次痛苦之后,我已经意识到我不能只操纵标准输入,并且我需要TextWatcher在我的一个EditText字段上使用捕获扫描数据以便我可以操作它.
我的问题是我无法弄清楚如何做到这一点.这是我有的:
activity_main.xml中
<LinearLayout>
<TextView />
<EditText android:id="@+id/datafield01" />
<EditText android:id="@+id/datafield02" />
<EditText android:id="@+id/datafield03" />
<Button />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText dataField01 = (EditText)findViewById(R.id.datafield01);
EditText dataField02 = (EditText)findViewById(R.id.datafield02);
EditText dataField02 = (EditText)findViewById(R.id.datafield03);
dataField01.addTextChangedListener(editTextWatcher);
}
TextWatcher editTextWatcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int …Run Code Online (Sandbox Code Playgroud) 我需要检测EditText中的文本更改.我已经尝试过TextWatcher,但是它没有按照我期望的方式工作.采用onTextChanged方法:
public void onTextChanged( CharSequence s, int start, int before, int count )
假设我在EditText中已经有了"John"文本.如果按另一个键,"e",s将是"Johne",start将是0,before将是4,并且count将是5.我希望这种方法工作的方式将是EditText之前的内容与什么之间的区别它即将成为.
所以我希望:
s = "Johne"
start = 4 // inserting character at index = 4
before = 0 // adding a character, so there was nothing there before
count = 1 // inserting one character
Run Code Online (Sandbox Code Playgroud)
我需要能够在每次按下按键时检测个别更改.因此,如果我有文本"John",我需要知道索引4处添加了"e".如果我退格"e",我需要知道"e"已从索引4中删除.如果我将光标放在"J"之后"和退格,我需要知道"J"已从索引0中删除.如果我把"G"放在"J"中,我想知道"G"在索引0处替换为"J".
我怎样才能做到这一点?我想不出一个可靠的方法来做到这一点.
我有3个EditText元素,如果输入中有4个字符,我想从一个字段跳到下一个字段.
我使用TextWatcher:
getEditView(R.id.edit_code1).addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable input) {
if(input.length() == 4){
getEditView(R.id.edit_code2).requestFocus();
}
}
});
Run Code Online (Sandbox Code Playgroud)
EditText的inputType是"textCapCharacters"
当对一个键进行长按以获得一个数字时,比如按住R得到一个4,大多数设备都不会添加该字母,直到用户停止按住该按钮,并且在选择字母4之后将触发afterTextChanged.在HTC键盘上(在这种情况下是2.3.5上的HTC Desire),情况并非如此.即使用户仍然按住按钮,R也被添加到EditText并触发afterTextChanged,代码完成其工作并将焦点放在下一个字段上.
如何防止这种不良行为?观看onKeyDown/Up无济于事,因为它没有注册正常的击键.
每次我调用notifyDataSetChanged编辑beforeTextChanged文本的焦点都会返回到 ListView 内的第一个编辑文本。
你遇到过这样的错误吗?如果是的话,您能给我一些关于如何处理这个问题的建议吗?
提前致谢。
我正在开发一个用户可以将内容发布到Feed的应用程序.在我的编辑文本(用于撰写)中,文本颜色为灰色.但是,当用户键入一个哈希标记,例如#help时,我需要在键入时将该文本着色为黑色,因此当用户键入"#"时,文本必须为黑色,直到它们开始一个新单词,然后文本颜色需要还原灰色
我一直在尝试使用文本观察器和spannable字符串进行着色.
这是我用textwatcher做的 onTextChanged
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Remove Text watcher to prevent infinite look
mTextField.removeTextChangedListener(contentFieldWatcher);
String startChar = null;
startChar = Character.toString(s.charAt(start));
L.i(getClass().getSimpleName(), "CHARACTER OF NEW WORD: " + startChar);
if (startChar.equals("#")) {
tagCheck(s.toString().substring(start), start, start + count);
}
}
Run Code Online (Sandbox Code Playgroud)
tagCheck方法
private void tagCheck(String s, int start, int end) {
mSpannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.black_colour)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Run Code Online (Sandbox Code Playgroud)
mSpannable是一个Spannable.
这种方法的问题是'#'显示为startChar,但是当用户键入下一个字符(符号或字母)时,它显示为startChar.用户键入圣诞老人的情况仍然是startChar.所以我面临的问题是如何在用户键入主题标签时动态着色文本.
所以普通字母可以正常工作,但是当你使用符号时它不会.我希望这个问题很清楚..我已经看了几天这一切都变得朦胧:)
当一个字符在 EditText 中被删除时,我想得到一个回调。
我该怎么做?
我对 Kotlin 中的内存管理有基本的了解,即局部变量存储在堆栈中并由操作系统管理,对象在堆上创建并由 JVM 管理它们。此外,对象作为引用的副本传递,原始类型作为函数中的副本传递。
在下面的代码中,我创建了一个 TextEditOperation 函数,该函数以编程方式创建 EditText 并使用指令对象设置其属性,并向其添加 TextWatcher,最后将 edittext 视图添加到父相对布局。
public fun TextEditOperation (pInstructionSetIndex: Int, pInstructionIndex: Int): View?
{
val instruction: InstructionKernelAndroid
val context: Context?
val activity: Activity
val textedit: EditText
val parent_view: RelativeLayout
// fetch the instruction
instruction = InstructionSetMgr.uAndroidOSInstructionSetList[pInstructionSetIndex][pInstructionIndex]
// get activity's Context
context = ProcessStates.GetMainActivityContext ()
// cast activity's context to activity to use it's method
activity = context as Activity
// get parent view
parent_view = activity.findViewById (instruction.GetParentID ()) as RelativeLayout
// create …Run Code Online (Sandbox Code Playgroud)