android 没有调用 textchangelistener
android textchangelistener is not called
我有以下问题:
在一个片段中,假设是 FragmentTest,我有以下线程:
new Thread(new Runnable() {
public void run() {
...
if (condition) {
if (isAdded()) {
getActivity.runOnUiThread(new Runnable () {
public void run() {
rightEditText.addTextChangedListener(FragmentTest.this);} }
}
}
}
...
}
}).start;
onCreate 方法内部。
FragmentTest实现了TextWatcher接口,但调用了none接口方法
public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText edttext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edttext = (EditText) findViewById(R.id.editText);
edttext.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"change",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void afterTextChanged(Editable editable) {
}
}
正在将评论迁移到答案:
Why do you need to do this inside a separate thread? I think the problem is that all the UI components run on the main UI thread. Try it without the thread.
我想这里不需要进一步的解释。 OP 将他的代码迁移到 UI 线程并且成功了。
我有以下问题:
在一个片段中,假设是 FragmentTest,我有以下线程:
new Thread(new Runnable() {
public void run() {
...
if (condition) {
if (isAdded()) {
getActivity.runOnUiThread(new Runnable () {
public void run() {
rightEditText.addTextChangedListener(FragmentTest.this);} }
}
}
}
...
}
}).start;
onCreate 方法内部。
FragmentTest实现了TextWatcher接口,但调用了none接口方法
public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText edttext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edttext = (EditText) findViewById(R.id.editText);
edttext.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"change",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void afterTextChanged(Editable editable) {
}
}
正在将评论迁移到答案:
Why do you need to do this inside a separate thread? I think the problem is that all the UI components run on the main UI thread. Try it without the thread.
我想这里不需要进一步的解释。 OP 将他的代码迁移到 UI 线程并且成功了。