我需要找到EditText中插入的新字符。实际上我使用TextChange Listener来查找文本字段中新输入的字符。例如,如果我输入单词"成功"逐一。我需要得到新进入的角色。如果我在e旁边插入X,我需要检测到X字符。
请为我提供最佳解决方案
答案 0 :(得分:2)
我认为您必须在edittext上尝试 TextChangedListener 来控制文本输入
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEdit = (EditText) findViewById(R.id.editText1);
viewText = (TextView) findViewById(R.id.text);
txtEdit.addTextChangedListener (new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.i("TC", "beforeTC " + s.toString() + " "
+ s.subSequence(start, start + count).toString());
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("TC", "onTC " + s.toString() + " "
+ s.subSequence(start, start + count).toString());
}
public void afterTextChanged(Editable s) {
Log.i("TC", "afterTC " + s.toString());
}
});
}
答案 1 :(得分:0)
您可以使用文字观察器。
public class MainActivity extends Activity implements TextWatcher
{
EditText gtex;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gtex=(EditText)findViewById(R.id.gtex);
gtex.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable p1)
{
// TODO: Implement this method
}
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void onTextChanged(CharSequence s, int a1, int a2, int a3)
{
if(a1+a2>=0&&(a1+a2<s.length())){
char ch=s.charAt(a1+a2);
};
//do stuff with ch
}
}