如何在monodroid

时间:2015-09-23 05:17:24

标签: android xamarin xamarin.android

我是xamarin的新用户,任何人都建议我如何格式化xxx-xxx-xxxx格式的电话号码。我在code snippet中实现了以下textwatcher,但是当用户按下键盘上的按钮时无法捕获。

 public PhoneNumberTextWatcher(activity context)
    {
        this.context = context;
    }
    public void AfterTextChanged(IEditable s)
    {

            if (s.Length() == 3 || s.Length() == 7)
            {
                var phone = s.ToString() + Char.ConvertFromUtf32(45);
                context.Phone.Text = phone;
                context.Phone.SetSelection(s.Length()+1);
            }



    }   

    public void BeforeTextChanged(Java.Lang.ICharSequence s, int start, int count, int after)
    {

    }

    public void OnTextChanged(Java.Lang.ICharSequence s, int start, int before, int count)
    {

    }

感谢。

3 个答案:

答案 0 :(得分:0)

试试这个,

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.formatnumber.FormatNumber" >

    <Button
        android:id="@+id/formate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Formate" />

</LinearLayout>

和Main.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FormatNumber extends Activity {

    Button btnFormate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_format_number);

        btnFormate=(Button)findViewById(R.id.formate);

        btnFormate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "formate : "+formatNumbersAsCode("1234567890"), Toast.LENGTH_LONG).show();
            }
        });

    }

    private String formatNumbersAsCode(CharSequence s) {
        return String.format("%s-%s-%s",((String) s).substring(0,3),((String) s).substring(3,6),((String) s).substring(6));
    }
}

答案 1 :(得分:0)

试试这个。

public PhoneNumberTextWatcher(activity context)
{
    this.context = context;
}

int prevLeng;
boolean isBackPressed;

public void AfterTextChanged(IEditable s)
{

        if(isBackPressed)
        {
            if (s.Length() == 4 || s.Length() == 8)
           {
            var phone = s.ToString().Substring(0,s.Length()-1);
            context.Phone.Text = phone;
            context.Phone.SetSelection(s.Length()-1);
           }
        }
        else if (s.Length() == 3 || s.Length() == 7)
        {
            var phone = s.ToString() + Char.ConvertFromUtf32(45);
            context.Phone.Text = phone;
            context.Phone.SetSelection(s.Length()+1);
        }
}   

public void BeforeTextChanged(Java.Lang.ICharSequence s, int start, int count, int after)
{
    prevLeng = s.Length();
}

public void OnTextChanged(Java.Lang.ICharSequence s, int start, int before, int count)
{
    if(prevLeng > s.Length())
        isBackPressed = true;
    else
        isBackPressed = false;
}

答案 2 :(得分:0)

Finally i got solution ....

 static int curserposition=0;
        bool backspace;
        string separator = char.ConvertFromUtf32(45);


        public void AfterTextChanged(IEditable s)
        {
            if (s.Length() > curserposition)
            {
                if (s.Length() == 3 || s.Length() == 7)
                {
                    backspace = false;
                    var phone = s.ToString() + separator;
                    context.edit_Phone.Text = phone;
                    context.edit_Phone.SetSelection(s.Length() + 1);

                }
            }
            else
            {
                if (s.Length () != 0) {
                    backspace = true;
                }
            }
            if(backspace)
            {
                if (s.Length() > curserposition)
                {
                    if (s.Length() == 8)
                    {
                        string value=s.ToString().Replace("-", "");
                        var phone = value.ToString().Substring(0, 3) + separator + value.ToString().Substring(3, 3) + separator + value.ToString().Substring(6);
                        context.edit_Phone.Text = phone;
                        context.edit_Phone.SetSelection(s.Length() + 1);
                        backspace = false;
                    }
                    if(s.Length()==4)
                    {
                        string value = s.ToString().Replace("-", "");
                        var phone = value.ToString().Substring(0, 3) + separator + value.ToString().Substring(3);
                        context.edit_Phone.Text = phone;
                        context.edit_Phone.SetSelection(s.Length() + 1);
                        backspace = false;
                    }
                }
            }

            curserposition = s.Length();
        }   

        public void BeforeTextChanged(Java.Lang.ICharSequence s, int start, int count, int after)
        {

        }

        public void OnTextChanged(Java.Lang.ICharSequence s, int start, int before, int count)
        {

        }
    }