【问题标题】:EditText get value to pass TextviewEditText 获取值以传递 Textview
【发布时间】:2014-05-20 04:59:57
【问题描述】:

我正在从事一项活动,其中我采用了 edittext 和 textview。我必须从 edittext 获取整数值并将其设置在 textview 上。

我必须在 edittext 上取 10 位数字(手机号),一旦 edittext 列获得 10 位数字,它会自动在同一活动中的 textview 上设置 10 位数字。

我是这样实现的

  1. 使用编辑文本获取价值

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

    在字符串中。

  2. 对其应用条件(str.length == 10) 并在 textview 上获取它。

由于上述过程不起作用,所以请尝试帮助我的朋友。

我正在处理这段代码:

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

                }
            });

        }
    }

【问题讨论】:

  • 为什么不工作?您面临的问题是什么?
  • 我必须在不点击任何内容的情况下将 10 位数的手机号码从 edittext 获取到 textview。一旦 edittext 采用 10 位数字,它就会自动在 textview 上设置这 10 位数字。

标签: java android android-edittext textview


【解决方案1】:

我会尝试:

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

            }

如果您的电话号码格式为“(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);
                }

            } 

【讨论】:

  • 您可能想尝试将代码放在onTextChanged 中
猜你喜欢
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
相关资源
最近更新 更多