我有EditText
"text = 0.00"
。当我按下数字3时,它应该是0.03
,第二次按下数字5时,文本应该是0.35
。 35.0
,35.09
就像这样。 EditText
最初的值为0.00
。
这些都是使用相同的EditText
完成的。
我如何实现这一目标?我尝试将addTextChangedListener()
与TextWatcher()
一起使用。
答案 0 :(得分:86)
你也可以试试这个:
EditText searchTo = (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
doSomething();
}
});
答案 1 :(得分:36)
您选择了正确的方法。您必须使用TextWatcher扩展该类并覆盖afterTextChanged()
,beforeTextChanged()
,onTextChanged()
。
您必须使用afterTextChanged()
方法编写所需的逻辑,以实现您所需的功能。
答案 2 :(得分:8)
在这里,我写了类似于你需要的东西:
inputBoxNumberEt.setText(". ");
inputBoxNumberEt.setSelection(inputBoxNumberEt.getText().length());
inputBoxNumberEt.addTextChangedListener(new TextWatcher() {
boolean ignoreChange = false;
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (!ignoreChange) {
String string = s.toString();
string = string.replace(".", "");
string = string.replace(" ", "");
if (string.length() == 0)
string = ". ";
else if (string.length() == 1)
string = ". " + string;
else if (string.length() == 2)
string = "." + string;
else if (string.length() > 2)
string = string.substring(0, string.length() - 2) + "." + string.substring(string.length() - 2, string.length());
ignoreChange = true;
inputBoxNumberEt.setText(string);
inputBoxNumberEt.setSelection(inputBoxNumberEt.getText().length());
ignoreChange = false;
}
}
});
答案 3 :(得分:1)
将逻辑放入
afterTextChanged(Editable s) {
string str = s.toString()
// use the string str
}
答案 4 :(得分:0)
更改文字;
multipleLine.addTextChangedListener(new 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) {
MainActivity.myArray.set(pickId,String.valueOf(s));
MainActivity.myAdapt.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
答案 5 :(得分:0)
在Kotlin中,使用设置Android EditText侦听器,
const axios = require('axios');
const fs = require('fs');
var https = require('https');
const httpagent = new https.Agent({
rejectUnauthorized: false,
ca: fs.readFileSync('path to rootCA.cer'),
})
//sslrootCAs.inject();
axios.get(
`https://REST_API_URL`, { httpagent }
)
答案 6 :(得分:0)
这是可用的 Kotlin 版本。
yourEditText.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
// TODO Auto-generated method stub
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// TODO Auto-generated method stub
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
doSomething()
}
})