問題描述
我有一些用于手機號碼輸入的 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左側的drawable設置為
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>
最終輸出的樣子
這篇關于設置不可更改的editText android的某些部分的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!