public class SampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepicker);
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
text1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
int total = Integer.parseInt(s.toString()) + 1;
text2.setText(String.valueOf(total));
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) {
}
});
text2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
int total = Integer.parseInt(s.toString()) + 2;
text1.setText(String.valueOf(total));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
您好我面临一个问题,多个编辑文本更新并行。如果我在第一个编辑文本中输入值,第二个编辑文本已更改。在第二个edittext中。但是编辑文本中光标的关键监听器存在问题。我无法得到光标。如何解决这个问题?
答案 0 :(得分:1)
我不知道你在做什么(或试图做什么)
在onTextChanged
text1
中,您正在触发onTextChanged
的{{1}}。看起来像是在这里触发无限文本更改听众
正在发生的事情的逻辑 - >
假设用户将字符text2
输入1
。 text1
现在是total
。现在,您将2
设置为text2
total
。这反过来会触发2
的文本更改侦听器。现在text2
是total
+ 2 = 2
。您现在将4
设置为4
的文本。这反过来会触发text1
的文本更改侦听器
对我来说似乎是一个永无止境的无限循环。这就是你得到text1
错误的原因(笑)你真正想做什么?
不确定可以解决该问题的原因(我手头没有Android Studio的工作副本),但将块移到StackOverflow
可能会这样做。
答案 1 :(得分:1)
你可以试试这种方式。 TextWatcher将通过AfterTextChanged方法通过Handler将值更新为第二个EditText。
EditText text1;
StringBuffer previousChar=new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
text1=(EditText)findViewById(R.id.editText);
text1.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) {
}
@Override
public void afterTextChanged(Editable s) {
if(!previousChar.toString().equals(s.toString())){
Message msg=new Message();
msg.obj=s.toString();
localHandler.sendMessage(msg);
previousChar=new StringBuffer(s.toString());
Log.i("TAG", "TextEntered = "+s);
}
}
});}
处理程序将用于更新第二个EditText中的文本
private Handler localHandler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
String value=(String)msg.obj;
text2.setText(value);
}
};
答案 2 :(得分:0)
includeTW = 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) {
if(!(includedText.getText().toString().equals("")))
{
excludedText.removeTextChangedListener(excludeTW);
String included = includedText.getText().toString();
excludedText.setText(included); //////// Error Line
excludedText.addTextChangedListener(excludeTW);
}
}
};
excludeTW = 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) {
if(!(excludedText.getText().toString().equals(""))) {
includedText.removeTextChangedListener(includeTW);
String excluded = excludedText.getText().toString();
includedText.setText(excluded);
includedText.addTextChangedListener(includeTW);
}
}
};
includedText.addTextChangedListener(includeTW) ;
excludedText.addTextChangedListener(excludeTW);
这将给出我想要的解决方案。我在每个edittext中移动光标获得值。
答案 3 :(得分:0)
当第一个EditText处于有效模式时删除另一个EditText的TextChangeListener,并在文本更改后添加它。
<?php
class UrlManager extends CUrlManager
{
public function createUrl($route,$params=array(),$ampersand='&')
{
if (!isset($params['language']))
{
if (Yii::app()->user->hasState('language'))
Yii::app()->language = Yii::app()->user->getState('language');
else if(isset(Yii::app()->request->cookies['language']))
Yii::app()->language = Yii::app()->request->cookies['language']->value;
$params['language']=Yii::app()->language;
}
return parent::createUrl($route, $params, $ampersand);
}
}
?>