EditText with not-editable/not-cancellable suffix(带有不可编辑/不可取消后缀的 EditText)
问题描述
我为我的一个活动创建了一个布局,用户可以在其中在一些 EditText 小部件中插入一个值.我需要其中一些 EditText 必须具有不可编辑的后缀(如 cm、mm 等).用户插入值后,我将解析这些 EditText 的内容,避免使用后缀,因此我将处理唯一没有后缀的输入.该怎么做?
i created a layout for one of my activities in which users can insert a value in some EditText widget. I need that some of these EditText must have a suffix (like cm, mm and so on) that has to be not editable. After the user has inserted the value i will parse the content of these EditText avoiding the suffix so i will handle the only input without the suffix. How to do that?
我已经在 SO 上进行了搜索和搜索,但没有任何帮助.我发现像这样的答案 https://stackoverflow.com/a/20794581/2516399 对我没有帮助.
I have already searched and searched here on SO but nothing helped me. I found answers like this one https://stackoverflow.com/a/20794581/2516399 that don't help me.
我希望我的问题很清楚......对不起我的英语
I hope i was clear in my question... sorry for my english
推荐答案
这是我的解决方案:一个在文本后面绘制后缀的 EditText 类.有两个自定义属性用于定义后缀的文本和后缀填充(到 EditText 的左角).
This is my solution: An EditText class that draws the suffix behind the text. There are two custom attributes for defining the text of the suffix and the suffix padding (to the left corner of the EditText).
public class EditTextWithSuffix extends EditText {
TextPaint textPaint = new TextPaint();
private String suffix = "";
private float suffixPadding;
public EditTextWithSuffix(Context context) {
super(context);
}
public EditTextWithSuffix(Context context, AttributeSet attrs) {
super(context, attrs);
getAttributes(context, attrs, 0);
}
public EditTextWithSuffix(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getAttributes(context, attrs, defStyleAttr);
}
@Override
public void onDraw(Canvas c){
super.onDraw(c);
int suffixXPosition = (int) textPaint.measureText(getText().toString()) + getPaddingLeft();
c.drawText(suffix, Math.max(suffixXPosition, suffixPadding), getBaseline(), textPaint);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
textPaint.setColor(getCurrentTextColor());
textPaint.setTextSize(getTextSize());
textPaint.setTextAlign(Paint.Align.LEFT);
}
private void getAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithSuffix, defStyleAttr, 0);
if(a != null) {
suffix = a.getString(R.styleable.EditTextWithSuffix_suffix);
if(suffix == null) {
suffix = "";
}
suffixPadding = a.getDimension(R.styleable.EditTextWithSuffix_suffixPadding, 0);
}
a.recycle();
}
}
这是属性定义:
<resources>
<declare-styleable name="EditTextWithSuffix">
<attr name="suffix" format="string|reference" />
<attr name="suffixPadding" format="dimension" />
</declare-styleable>
</resources>
这篇关于带有不可编辑/不可取消后缀的 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有不可编辑/不可取消后缀的 EditText
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01