简体   繁体   English

EditText获取值以传递Textview

[英]EditText get value to pass Textview

I am working on an activity in which I have taken edittext and textview. 我正在从事一项从事edittext和textview的活动。 I have to get integer value from edittext and set it on textview. 我必须从edittext获取整数值并将其设置在textview上。

I have to take 10-digit number(mobile no) on edittext and as soon as the edittext column gets 10-digits,its automatically sets 10-digit number on textview within the same activity. 我必须在edittext上输入10位数字(手机号码),并且edittext列一旦获得10位数字,它就会在同一活动中自动在textview上设置10位数字。

I have implemented in this manner 我已经以这种方式实施

  1. Getting value on edittext using 使用EditText获得价值

     Integer.parseInt(edittext.getText().toString()); 

    in a String str. 在String str。

  2. Applying condition to it (str.length == 10) and getting it on textview. 将条件应用于它(str.length == 10)并在textview上获取它。

As the above process is not working so try to help me friends. 由于上述过程无法正常进行,请尝试帮助我的朋友。

I am working on this code: 我正在这段代码上:

public class MobileNo extends Activity {

        EditText et;
        TextView tv;
        int i;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            et = (EditText) findViewById(R.id.edit);
            tv = (TextView) findViewById(R.id.text1);

            et.getText().toString();

            et.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable textVal) {
                    // TODO Auto-generated method stub
                    Editable inputNo = textVal;
                    if (inputNo.length() == 10) {
                        tv.setText(inputNo);
                    }

                }
            });

        }
    }

I would try: 我会尝试:

            @Override
            public void afterTextChanged(Editable textVal) {
                String inputNo = textVal.toString();
                if (inputNo.length() == 10) {
                    tv.setText(inputNo);
                }

            }

If your phone number is formatted like "(800) 549-2992" or "800-549-2992" and you only wants the numbers then you'd have to do the following: 如果您的电话号码格式设置为“(800)549-2992”或“ 800-549-2992”,而您只想要该号码,则必须执行以下操作:

            @Override
            public void afterTextChanged(Editable textVal) {
                // takes out all non-digits from string
                String inputNo = textVal.toString().replaceAll("[^\\d]","");
                if (inputNo.length() == 10) {
                    tv.setText(inputNo);
                }

            } 

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

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