【问题标题】:How to count multiple phone number and sms number from editText field in android?如何从android中的editText字段中计算多个电话号码和短信号码?
【发布时间】:2017-04-01 06:48:12
【问题描述】:

我面临两个问题。 1.从editText字段中计算多个电话号码。 2. 跨越特定字符数后统计短信号码。

<android.support.design.widget.TextInputLayout
                android:id="@+id/phoneContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:counterEnabled="true"
                app:counterMaxLength="50">

                <com.andreabaccega.widget.FormEditText
                    android:id="@+id/idContacts"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Recipients"
                    android:ems="10"
                    android:inputType="phone"
                    android:padding="15dp" />
</android.support.design.widget.TextInputLayout>

在这里,我可以计算电话号码的字符。我需要计算整个电话号码。例如:如果用户在电话编辑字段中输入 012346771233,02546547589,它将计为 2。但在我的情况下,它是计数单个字符。我搜索了很多,但找不到任何解决方案。

其次,我有一个editText字段,用户可以在其中输入消息。当用户越过字符的特定数字(如160个字符)时,它将执行2个操作。它将增加短信数量并设置字符数(如146个字符) ) 下一条短信。

<android.support.design.widget.TextInputLayout
                    android:id="@+id/textContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:counterEnabled="true"
                    app:counterMaxLength="160">

                    <com.andreabaccega.widget.FormEditText
                        android:id="@+id/idMsg"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:ems="10"
                        android:hint="@string/message"
                        android:inputType="text|textMultiLine|textAutoCorrect"
                        android:padding="10dp"
                        whatever:emptyErrorString="Message can not be empty."
                        whatever:testType="alpha" />
</android.support.design.widget.TextInputLayout>

在我的情况下,我可以计算字符,但我如何计算短信数量并为下一个短信设置 counterMaxLength?

【问题讨论】:

  • 第一个,写一个onchange监听器,得到EditText的值,用.(点)分割字符串,求它的长度。对于第二个,编写一个onChange 侦听器并获取EditText 的值并找到字符串长度并在那里实现您的160char 逻辑。
  • 感谢您的快速回复。我明白了。请检查链接[链接]unsee.cc/domebuga您能告诉我如何设置红色框的值吗?
  • 我希望它是一个简单的TextView,在第一个 onChange 侦听器中使用代码中的findViewById 获取它的引用,并使用setText 给它你想要的值。比如说recipients.setText(2/50)
  • 谢谢...我得到了答案... :)

标签: android xml


【解决方案1】:

对于您的第一个问题,您可以在 EditText onchange 监听器中进行计算

yourEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (s.length() >= 0) {

            String []phnArray = s.toString().split(",");
            int phCount  = phnArray.length;
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

对于你的第二个问题,我有点不清楚你真正想要什么。

如果你想检查长度是否超过 160,那么你可以检查

if(s.length > 160){
    //do your require changes here
} 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多