我是xamarin新手,如果有人建议我如何将电话号码格式化为xxx-xxx-xxxx格式,请告诉我。我已经在textwatcher中实现了以下code snippet,但当用户按下键盘上的后退按钮时无法捕获。
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)
{
}谢谢。
发布于 2015-09-23 13:49:34
尝尝这个,
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));
}
}https://stackoverflow.com/questions/32731484
复制相似问题