每次按键都会更新 TextView

发布于 2024-11-16 19:13:43 字数 1901 浏览 1 评论 0原文

我想做的是用每个关键事件更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-11-23 19:13:43

你在哪里应用这个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.

舂唻埖巳落 2024-11-23 19:13:43

OnKeyListener 不适用于软键盘。您应该实现 TextWatcher 来跟踪文本更改。另请参阅此处

OnKeyListener does not work with soft keyboard. You should implement TextWatcher to track text changes. Also look here.

朮生 2024-11-23 19:13:43

我刚刚使用以下方法解决了同样的问题:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class InputField extends AppCompatActivity {

private EditText input;
private TextView displayString;
private String writeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_field);

    input = (EditText) findViewById(R.id.editText);
    displayString = (TextView) findViewById(R.id.displayStringText);

    input.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) {
            writeText = input.getText().toString();
            displayString.setText(writeText);
        }
    });

  }
}

I just solved this same issue using the following:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class InputField extends AppCompatActivity {

private EditText input;
private TextView displayString;
private String writeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_field);

    input = (EditText) findViewById(R.id.editText);
    displayString = (TextView) findViewById(R.id.displayStringText);

    input.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) {
            writeText = input.getText().toString();
            displayString.setText(writeText);
        }
    });

  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文