implement uitextfield properties in android

后端 未结 1 1385
-上瘾入骨i
-上瘾入骨i 2021-01-19 22:05

I am an android and iphone developer. I am a newbie though working on this huge project. I am trying to implement the leftView, leftviewmode,rightview,rightviewmode properti

相关标签:
1条回答
  • 2021-01-19 23:06

    This code will give you an Android EditText field that functions exactly like an iPhone UITextField with options for clearbuttonmode, leftview, rightview, leftviewmode, rightviewmode, UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways

        String value = "";//any text you are pre-filling in the EditText
        final String viewMode = "editing";//never | editing | unlessEditing | always
        final String viewSide = "right"; //left | right
        final EditText et = new EditText(this);
        et.setText(value);
        //your leftview, rightview or clearbuttonmode image. for clearbuttonmode this one from standard android images looks pretty good actually
        final Drawable x = getResources().getDrawable(R.drawable.presence_offline);
        x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
        Drawable x2 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?(value.equals("") ? null : x):viewMode.equals("unlessEditing")?(value.equals("") ? x : null):null;
        et.setCompoundDrawables(viewSide.equals("left")?x2:null, null, viewSide.equals("right")?x2:null, null);
        et.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (et.getCompoundDrawables()[viewSide.equals("left")?0:2] == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                //x pressed
                if ((viewSide.equals("left")&&event.getX() < et.getPaddingLeft() + x.getIntrinsicWidth())
                        ||(viewSide.equals("right")&&event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth())) {
                    Drawable x3 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?null:viewMode.equals("unlessEditing")?x:null;
                    et.setText("");
                    et.setCompoundDrawables(viewSide.equals("left")?x3:null, null, viewSide.equals("right")?x3:null, null);
                }          
                return false;
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Drawable x4 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?(et.getText().toString().equals("") ? null : x):viewMode.equals("unlessEditing")?(et.getText().toString().equals("") ? x : null):null;
               et.setCompoundDrawables(viewSide.equals("left")?x4:null, null, viewSide.equals("right")?x4:null, null);
            }
    
            @Override
            public void afterTextChanged(Editable arg0) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
        });
    
    0 讨论(0)
提交回复
热议问题