怎样才能让我瞬间从一个TextView一个EditText显示文本? [英] How can I instantly display text from an Edittext in a TextView?

查看:168
本文介绍了怎样才能让我瞬间从一个TextView一个EditText显示文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想这样做简单的事:我有一个EditText和一个TextView。现在,我想即刻(逐个字符)显示是在TextView中的EditText上输入的文本。另外,当退格键被pressed和一个字符内从editext删除,这也应该发生在TextView的

I am basically trying to do this simple thing: I have got an Edittext and a TextView. Now I would like to instantly (character by character) display the text that is entered in the edittext in the textview. In addition, when the backspace key is pressed and one charcter is deleted from the editext, this should also happen in the textview.

总之:我想在TextView中立即显示一切从EditText上。我该怎么做?

In short: I want to display everything from the edittext immediately in the textview. How can I do that?

下面是一些code,我试过,但没有为我工作。当我在使用Android 4.0模拟器中运行它(并使用计算机键盘进行输入),在TextView中显示的每一个字符两次,并没有在任何的EditText文本。
在另一方面,当我使用了Android 2.3.3模拟器(和使用仿真键盘进行输入)没有在TextView中没有文字,但是EditText上正常工作。

Here is some code that I tried, but that didn't work for me. When I run it on a emulator with Android 4.0 (and use the computer keyboard for input), every character is displayed twice in the textview and there is no text in the edittext. On the other hand, when I use an emulator with Android 2.3.3 (and use the emulator keyboard for input) there is no text in the textview, however the edittext works fine.

的onCreate()我把我的方法使用setListener()。这看起来是这样的:

In onCreate() I call my method setListener(). This looks like this:

private void setListener() {
    mEdittext.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            String str = mTextview.getText().toString();
            if(keyCode == KeyEvent.KEYCODE_DEL && str.length() > 0) {
                str = str.substring(0, str.length()-1);
            } else {
                char newChar = (char)event.getUnicodeChar();
                str = str.concat(Character.toString(newChar)) ;
            }
            mTextview.setText(str);

            return true;
        }
    });
}

任何想法来解决这个问题?

Any ideas to fix this?

推荐答案

您可以使用 TextWatcher 来设置的TextView作为输入文本:

You can use the TextWatcher to set the TextView as text is entered:

TextWatcher watcher = new TextWatcher() {
        public void afterTextChanged(Editable s) { 
            mTextview.setText(mEdittext.getText().toString());          
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
    };

    mEdittext.addTextChangedListener(watcher);

这篇关于怎样才能让我瞬间从一个TextView一个EditText显示文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆