简体   繁体   English

动态设置EditText的文本

[英]Setting text dynamically of EditText

I am unable to figure out how to set text dynamically of an EditText. 我无法弄清楚如何动态设置EditText的文本。 A fellow user at stackoverflow suggested me to use onTextChanged() but I don't how that either.So I read on it for an hour and when tried to implement it,Errors Errors Errors! stackoverflow的一位用户建议我使用onTextChanged()但我也不怎么用。所以我读了一个小时,然后尝试实现它,错误错误错误! So,I am asking this question again. 所以,我再次问这个问题。

I have two EditTexts et1 and et2. 我有两个EditTexts et1和et2。 Both are of type NumberDecimal .et2 is unfocusable and unclickable .The user can only input value in et1.What I want is that as soon as user enters a number in et1,that number should pe displayed in et2.So,if the user has to enter 10,he will first enter 1 and that 1 should also be displayed in et2.Now to complete the number,he will enter 0 and then the number 10 should be displayed in et2.So,et2 should be updated character by character depending upon the entry made in et1. 两者都是NumberDecimal类型。et2是unfocusableunfocusable unclickable 。用户只能在et1中输入值。我想要的是,只要用户在et1中输入数字,该数字就应该在et2中显示。因此,如果用户输入10,他将首先输入1,并且在et2中也应显示1。现在要完成该数字,他将输入0,然后应在et2中显示数字10。因此,et2应该根据字符进行逐字符更新。在et1中输入。

This is how I read from et1 : 这就是我从et1中读取的内容:

et1=(EditText) findViewById(R.id.et1);
et2=(EditText) findViewById(R.id.et2);
String read=et1.getText().toString();
if(read.length()!=0)
{
float a=Float.parseFloat(read);
}

Now if I use setText() on et2,that wont be dynamic and furthermore I will have to make use of an button to show the text in et2 and it will show whole String which was entered in et1 until the button was clicked. 现在,如果我在et2上使用setText() ,它将是动态的,此外,我将不得不使用button来显示et2中的文本,并且它将显示在et1中输入的整个String ,直到单击该按钮为止。

Using Listener on et1 is an good idea but I don't know its implementation with onTextChanged since I am a newbie. onTextChanged上使用Listener是一个好主意,但由于我是新手,所以我不知道它在onTextChanged实现。

Please help. 请帮忙。

Thanking you in anticipation. 感谢你在期待。

Use the below code 使用下面的代码

final TextView et2 = new TextView(this);
        TextView et1 = new TextView(this){
            @Override
            protected void onTextChanged(CharSequence text, int start,
                    int before, int after) {
                // TODO Auto-generated method stub
                super.onTextChanged(text, start, before, after);
                et2.setText(this.getText());
            }
        };

now add these et1,et2 to any layout. 现在将这些et1,et2添加到任何布局中。

I had to do the same thing you are trying to do, I used a KeyListener. 我必须做与您尝试做的相同的事情,我使用了KeyListener。 Just update the EditText everytime a key is pressed. 只需在每次按键时更新EditText。

Check it out: 看看这个:

http://developer.android.com/reference/android/text/method/KeyListener.html http://developer.android.com/reference/android/text/method/KeyListener.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM