我有一些EditText上的手机号码输入。应用程序必须为每个国家添加独特的文字。例如,对于亚美尼亚是必须添加 +374
,和用户必须填写其他号码。此外 +374
必须是不变的,用户不能更改或删除它。那么,有没有某种方式这样做?
I have some EditText for mobile number input. App must add unique text for every country. For example for Armenia is must add +374
, and user must fill other numbers. Also +374
must be unchangeable, user can't change or remove it. So is there some kind of ways for doing this?
编辑:
我不希望使用的TextView或另一种观点认为这一文本,并把它留给了ediText的。我想找到某种方式以较少的运算。我需要的文字被冻结不检查每一个文本更改或添加缺少的文本时,用户会删除它的某些部分。
I don't want to use textView or another view with this text and put it left of the ediText. I want to find some way with less operations. I need text to be frozen not to check every text changes or add missing text when user will delete some part of it.
创建一个自定义绘制类,这将有助于将文本转换为可绘制的。
Create a custom drawable class that will help to convert text into drawable.
public class TextDrawable extends Drawable {
private final String text;
private final Paint paint;
public TextDrawable(String text) {
this.text = text;
this.paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(16f);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.LEFT);
}
@Override
public void draw(Canvas canvas) {
canvas.drawText(text, 0, 6, paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
然后设置绘制到左的EditText为
Then set the drawable to left of the edittext as
EditText et = (EditText)findViewById(R.id.editText1);
String code = "+374";
et.setCompoundDrawablesWithIntrinsicBounds(new TextDrawable(code), null, null, null);
et.setCompoundDrawablePadding(code.length()*10);
凡的EditText在布局文件中定义为
Where the edittext is defined in the layout file as
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:ems="10" >
<requestFocus />
</EditText>
最终输出看起来像
Final Output looks like