开发者

Update TextView with each key press

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 unl开发者_如何学JAVAucky with getting switch statements to work. Any help please?


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 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:

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);
        }
    });

  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜