Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

android - EditText setError() with icon but without Popup message

I want to to have some validation for my EditText wherein I want to show "enter image description here" icon (that comes when you put editText.setError("blah blah")) but don't want the text in the popup displaying that "blah blah".

Is there any way to do it? One way is to create a custom layout which will show the image icon in the EditText. But is there any better solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem solved after a lot of research and permutations- (Also thanks to @van)

Create a new class that will extend EditText something like this-

public class MyEditText extends EditText {

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setError(CharSequence error, Drawable icon) {
    setCompoundDrawables(null, null, icon, null);
}
}

Use this class as a view in your xml like this-

<com.raj.poc.MyEditText
    android:id="@+id/et_test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Now in the third step, just set a TextWatcher to your custom text view like this-

    et = (MyEditText) findViewById(R.id.et_test);

    errorIcon = getResources().getDrawable(R.drawable.ic_error);
    errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
       et.setError(null,errorIcon);

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.toString().length()>6){
                et.setError("", null);
            }else{
                et.setError("", errorIcon);
            }
        }
    });

where R.drawable.ic_error = image

Keeping text null solves the problem But if we keep only null in setError(null), this won't show the validation error; it should be null along with second param.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...