gpt4 book ai didi

android - 无法在android中正确实现textwatcher

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:17 24 4
gpt4 key购买 nike

我正在寻找一种方法来找出用户在 EditText 中键入的内容,然后我偶然发现了 TextWatcher。问题是我不明白函数中的参数指的是什么。我想逐个字母地跟踪用户输入的文本。我不知道哪个参数是正确的下标号。我尝试了不同的变体,但都没有成功。

本质上,我想保存在数组中输入的文本,直到用户按下“,”按钮或超过 24 个字符的限制。请指教。

public class Main extends Activity implements OnClickListener {

// inputans.setOnKeyListener(this);

Button go;
TextView answer;
TextView check;
EditText inputans, inputq;
boolean done1 = false;
char[] ans = new char[30];
String predef = "Please answer my question";
char[] predefc = predef.toCharArray();
int len = predefc.length;
int i = 0;
char[] substring = new char[30];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialisevars();
go.setEnabled(false);
go.setOnClickListener(this);

inputans.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

if ((count - before) > 0) {
char current = s.charAt(before);
if (current == '.' || i >= 24) { // 24 check
// ans[i]='\0';
// inputans.setText(predef,
// TextView.BufferType.EDITABLE);
check.setText(predef);
go.setEnabled(true);
} else {

ans[i] = current;
i++; // char[] substring = char[30]; back button
// problem,check if prev length - current length
// >0

int m;
for (m = 0; m <= i; m++) {
substring[m] = predefc[m];
}

check.setText(substring.toString());
// substring[m]='\0';
/*
* inputans.setText(substring.toString(),
* TextView.BufferType.EDITABLE);
*/

}
}

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});

}

private void initialisevars() {
// TODO Auto-generated method stub
go = (Button) findViewById(R.id.bGo);
answer = (TextView) findViewById(R.id.tvAns);
inputans = (EditText) findViewById(R.id.etTricky);
inputq = (EditText) findViewById(R.id.etQues);
check = (TextView) findViewById(R.id.textView1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bGo: {

answer.setText(ans.toString());

}
break;

}
}

}

最佳答案

在 Android 文档中,您可以阅读到:

public abstract void onTextChanged (CharSequence s, int start, int before, int count) Added in API level 1

This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before. It is an error to attempt to make changes to s from this callback.

这意味着:

  • s 已经是一个修改过的文本(你可以得到它的长度,通过s.length())
  • count 是已添加的字符数
  • start 是添加新字符的位置
  • before 是更改前的文本长度

基本上为了实现你想要的,我宁愿使用这种方法而不是数组:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialisevars();
go.setEnabled(false);
go.setOnClickListener(this);

inputans.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

if ((s.length() - before) > 0) { // Check if text is longer than before changing
char current = s.charAt(before);
if (current == '.' || s.length() >= 24) {
check.setText(predef);
go.setEnabled(true);
} else {
check.setText(s);
}
}

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});
}

就这么简单。

关于android - 无法在android中正确实现textwatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16117893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com