在android中使用TextWatcher()
我正在构建一个应用程序,如果用户在编辑文本的开头输入“o”或“o”,则应执行以下操作: 在运行时,在EditText中将输入的“o”或“o”替换为“p”,并将TextView设置为“橙色” 类似地,如果用户输入“g”或“g”,则在运行时EditText中输入的“g”或“g”应替换为“p”,并将TextView设置为“绿色”。 等等。。。。 我如何才能做到这一点。我已经使用了TextWatcher(),但无法实现目标。将非常感谢您的帮助在android中使用TextWatcher(),android,textwatcher,Android,Textwatcher,我正在构建一个应用程序,如果用户在编辑文本的开头输入“o”或“o”,则应执行以下操作: 在运行时,在EditText中将输入的“o”或“o”替换为“p”,并将TextView设置为“橙色” 类似地,如果用户输入“g”或“g”,则在运行时EditText中输入的“g”或“g”应替换为“p”,并将TextView设置为“绿色”。 等等。。。。 我如何才能做到这一点。我已经使用了TextWatcher(),但无法实现目标。将非常感谢您的帮助 package com.example.answer; i
package com.example.answer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class data extends Activity implements OnClickListener {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
Button start, startFor;
EditText sendET;
TextView gotAnswer, result;
int flag = 1;
TextWatcher tt = null;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
final EditText sendET = (EditText) findViewById(R.id.sendet);
final TextView result = (TextView) findViewById(R.id.tv);
tt = new TextWatcher() {
public void afterTextChanged(Editable s) {
sendET.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt);
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt);
}
};
sendET.addTextChangedListener(tt);
}
private void initialize() {
start = (Button) findViewById(R.id.button1);
sendET = (EditText) findViewById(R.id.sendet);
start.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String bread = sendET.getText().toString();
result = (TextView) findViewById(R.id.tv);
result.setText(bread);
/*
* Bundle basket = new Bundle(); basket.putString("key", bread);
* Intent a = new Intent(data.this, openedclass.class);
* a.putExtras(basket); startActivity(a); break;
*/
}
}
}
这是错误的。这不是在Java中检查字符串相等性的方式。正确用法:
if(sendET.getText().toString().equals("o"))
此外,您还可以删除侦听器并再次添加它。我不知道你的应用程序逻辑,但我想你不想addTextChangedListener
:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt); // REMOVING
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt); // ADDING AGAIN
}
您是否有可能将您的问题分解为您认为运行不正常的方法?我认为这可能有助于用户调试你的应用程序。
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt); // REMOVING
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt); // ADDING AGAIN
}