Android如何同时将edittext的文本发送到textview的文本?

时间:2011-08-24 13:30:07

标签: android append

基本上,我想从EditText获取字符并添加TextView文本。例如,当我写你好时,TextView的文本在一个过程中发生了变化:

  1. ħ
  2. HEL
  3. 地狱
  4. 你好
  5. 那我怎么能这样做?我试过addTextChangedListener(),但我有一些错误,也许你的其他方法可以帮助我。

5 个答案:

答案 0 :(得分:1)

为您的edittext添加一个侦听器以侦听更改:

editText.addTextChangedListener(new TextWatcher{

    public void afterTextChanged(Editable s){
        // This method is called to notify you that, somewhere within s, 
        // the text has been changed.   
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after){
        // This method is called to notify you that, within s, 
        // the count characters beginning at start are about to be replaced by 
        // new text with length after.
    } 

    public void onTextChanged(CharSequence s, int start, int before, int count){
        // This method is called to notify you that, within s, 
        // the count characters beginning at start have just replaced old 
        // text that had length before.
    }
});

答案 1 :(得分:1)

如果要将edittext数据放入textview,则必须同时同步edittext字段,这可以通过使用线程来完成。在下面的代码中,Handler的实例与单个线程相关联。您可以通过更改postDelayed方法的第二个参数值,按照您定义的时间间隔同步背景

    Tv=(TextView)findViewById(R.id.tv);
    Et=(EditText)findViewById(R.id.et);


    final Handler handler=new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            Tv.setText(Et.getText().toString());
        handler.postDelayed(this,100);
        }
    },100);

答案 2 :(得分:0)

您可以使用OnKeyListenr。

所以你必须在editText中设置一个Listener:

myedittext.setOnKeyListener(new OnKeyListener() {

    @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) { 
                 //Here you update your textView
                 key = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
                 myTv.append(key.getDisplayLabel(keyCode)();
                 return true;
            }
});

我希望这对你有所帮助。 再见:)

答案 3 :(得分:0)

试试这个:

edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        finalTextView.setText(editText.getText());
    }
});

答案 4 :(得分:0)

打勾的答案是错误的...

entertxt.addTextChangedListener(new TextWatcher () {

            public void afterTextChanged(Editable s){
                // This method is called to notify you that, somewhere within s,
                // the text has been changed.
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after){
                // This method is called to notify you that, within s,
                // the count characters beginning at start are about to be replaced by
                // new text with length after.
            }

            public void onTextChanged(CharSequence s, int start, int before, int count){
                // This method is called to notify you that, within s,
                // the count characters beginning at start have just replaced old
                // text that had length before.
            }
        });
相关问题