每次按键都会更新 TextView
我想做的是用每个关键事件更新 TextView - 基本上将 EditText 的内容复制到 TextView。由于某种原因,它不起作用。代码如下:
View.OnKeyListener keyHandler = new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
TextView id = (TextView)findViewById(R.id.previewEmplIdData);
TextView fn = (TextView)findViewById(R.id.previewNameData);
TextView ttl = (TextView)findViewById(R.id.previewTitleData);
TextView mgr = (TextView)findViewById(R.id.previewMgrData);
TextView loc = (TextView)findViewById(R.id.previewLocData);
TextView strt = (TextView)findViewById(R.id.previewStartData);
String text = "";
EditText et;
switch(v.getId()){
case R.id.etFirstName:
et = (EditText)v; // must first switch to a text field
text = et.getText().toString();
fn.setText(text);
break;
case R.id.etLastName:
et = (EditText)v;
text = et.getText().toString();
break;
case R.id.etEmplId:
et = (EditText)v;
text = et.getText().toString();
id.setText(text);
break;
case R.id.etStartDate:
et = (EditText)v;
text = et.getText().toString();
strt.setText(text);
break;
case R.id.etTitle:
et = (EditText)v;
text = et.getText().toString();
ttl.setText(text);
break;
case R.id.spinnerLocation:
break;
case R.id.spinnerManager:
break;
}
return false;
}
};
基本上,我在按键时切换视图 id,然后将视图投射到 EditText。我不太确定为什么这不起作用......我一直很不幸地无法让 switch 语句发挥作用。有什么帮助吗?
What I am trying to do is update a TextView with each key event - basically copying the contents of the EditText to the TextView. For some reason, it's not working. Here's the code:
View.OnKeyListener keyHandler = new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
TextView id = (TextView)findViewById(R.id.previewEmplIdData);
TextView fn = (TextView)findViewById(R.id.previewNameData);
TextView ttl = (TextView)findViewById(R.id.previewTitleData);
TextView mgr = (TextView)findViewById(R.id.previewMgrData);
TextView loc = (TextView)findViewById(R.id.previewLocData);
TextView strt = (TextView)findViewById(R.id.previewStartData);
String text = "";
EditText et;
switch(v.getId()){
case R.id.etFirstName:
et = (EditText)v; // must first switch to a text field
text = et.getText().toString();
fn.setText(text);
break;
case R.id.etLastName:
et = (EditText)v;
text = et.getText().toString();
break;
case R.id.etEmplId:
et = (EditText)v;
text = et.getText().toString();
id.setText(text);
break;
case R.id.etStartDate:
et = (EditText)v;
text = et.getText().toString();
strt.setText(text);
break;
case R.id.etTitle:
et = (EditText)v;
text = et.getText().toString();
ttl.setText(text);
break;
case R.id.spinnerLocation:
break;
case R.id.spinnerManager:
break;
}
return false;
}
};
Basically I'm switching through the View id's on key press, and then casting the view to the EditText. I'm not really sure why this doesn't work...I've always been pretty unlucky with getting switch statements to work. Any help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在哪里应用这个
keyHandler
?如果您刚刚创建并初始化它,但没有在任何视图中使用,那么它根本不会运行。Where do you apply this
keyHandler
? If you just created and initialized it, but didn't used in any view, it won't run at all.OnKeyListener 不适用于软键盘。您应该实现 TextWatcher 来跟踪文本更改。另请参阅此处。
OnKeyListener does not work with soft keyboard. You should implement TextWatcher to track text changes. Also look here.
我刚刚使用以下方法解决了同样的问题:
I just solved this same issue using the following: