0

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?

Cody
  • 8,686
  • 18
  • 71
  • 126

3 Answers3

2

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.

Mykhailo Gaidai
  • 3,130
  • 1
  • 21
  • 23
  • Oh...that's probably what it is. How would I apply it to the current view? `getContext().setOnKeyListener()`? – Cody Jun 24 '11 at 14:29
  • 1
    If you want to apply it to the activity content view, it's better to change `setContentView(R.layout.activity_layout)` in the activity `onCreate` to something like `View activityView = LayoutInflater.from(this).inflate(R.layout.activity_layout); setContentView(activityView)` and after the creation of `keyHandler` use `activityView.setOnKeyListener(keyHandler);`. But the idea of @woodshy is actually better :) – Mykhailo Gaidai Jun 24 '11 at 14:42
  • As far as I remember, you can use `onKeyDown` event of the activity to intercept virtual keyboard. But you'll have to operate with keycodes, not strings. – Mykhailo Gaidai Jun 24 '11 at 14:45
  • Thanks for the help. I'm still having issues with it working correctly. I think I will check out woodshy's solution. Thanks! – Cody Jun 24 '11 at 15:49
2

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

Community
  • 1
  • 1
woodshy
  • 4,085
  • 3
  • 22
  • 21
  • Thanks. I didn't know this. I got it working and it force closed when I used the soft keyboard. – Cody Jun 24 '11 at 15:50
0

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

  }
}
rsmediapc
  • 21
  • 5