2017-07-18 84 views
1

我在一個佈局上有三個edittext。當它是空的時候,我刪除右邊的drawable。當它不是空的時候,我添加了drawable。當你點擊繪製時,我清除了edittext。Android Edittext可繪製點擊卡住的焦點

當我點擊鼠標右鍵時,光標卡住了,下劃線保持着色。很少有2-3個光標。爲什麼??我做得不好?

BAD: enter image description here

GOOD:enter image description here

的佈局:

<EditText 
     android:id="@+id/et1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableRight="@mipmap/ic_launcher"/> 

    <EditText 
     android:id="@+id/et2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableRight="@mipmap/ic_launcher"/> 

    <EditText 
     android:id="@+id/et3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableRight="@mipmap/ic_launcher"/> 

在MainActivity 公共類MainActivity延伸AppCompatActivity {

private DrawableState etstate1; 
private DrawableState etstate2; 
private DrawableState etstate3; 
private EditText et2; 
private EditText et1; 
private EditText et3; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    et1 = (EditText)findViewById(R.id.et1); 
    et2 = (EditText)findViewById(R.id.et2); 
    et3 = (EditText)findViewById(R.id.et3); 


    etstate1 = new DrawableState(et1); 
    etstate2 = new DrawableState(et2); 
    etstate3 = new DrawableState(et3); 

    et1.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 

      if (DrawableClick.isDrawableClick(motionEvent, et1, DRAWABLE_RIGHT)) { 
       et1.setText(""); 
       etstate1.refreshDrawable(); 
       return true; 
      } 
      return false; 
     } 
    }); 

    et2.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 

      if (DrawableClick.isDrawableClick(motionEvent, et2, DRAWABLE_RIGHT)) { 
       et2.setText(""); 
       etstate2.refreshDrawable(); 
       return true; 
      } 
      return false; 
     } 
    }); 

    et3.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 

      if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) { 
       et3.setText(""); 
       etstate3.refreshDrawable(); 
       return true; 
      } 
      return false; 
     } 
    }); 


    et1.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void afterTextChanged(Editable editable) { 
      etstate1.refreshDrawable(); 
     } 
    }); 

    et2.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void afterTextChanged(Editable editable) { 
      etstate2.refreshDrawable(); 
     } 
    }); 

    et3.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     @Override 
     public void afterTextChanged(Editable editable) { 
      etstate3.refreshDrawable(); 
     } 


    }); 

    } 
} 

Helper類:

public class DrawableClick { 


    public static boolean isDrawableClick(MotionEvent event, EditText editText, DrawablePositions drawablePosition) { 
     if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (editText.getCompoundDrawables()[drawablePosition.position] != null && event.getX() >= (editText.getRight() - editText.getLeft() - editText.getCompoundDrawables()[drawablePosition.position].getBounds().width())) { 
       return true; 
      } 
     } 
     return false; 
    } 


    public enum DrawablePositions { 
     DRAWABLE_LEFT(0), 
     DRAWABLE_TOP(1), 
     DRAWABLE_RIGHT(2), 
     DRAWABLE_BOTTOM(3); 

     private int position; 

     DrawablePositions(int pos) { 
      this.position = pos; 
     } 
     } 
    } 

下一頁:

公共類DrawableState {

private EditText editText; 
private Drawable[] editTextDrawables; 

public DrawableState(EditText editText) { 
    this.editText = editText; 
    saveDrawableState(); 
    refreshDrawable(); 
} 


public void saveDrawableState() { 
    this.editTextDrawables = editText.getCompoundDrawables(); 
} 

public void refreshDrawable() { 

    if (editText.getText().toString().equals("")) { 
     editText.setCompoundDrawables(null, null, null, null); 
     editText.clearFocus(); 
    } else { 
     if (editTextDrawables != null) { 
      editText.setCompoundDrawables(editTextDrawables[0], editTextDrawables[1], editTextDrawables[2], editTextDrawables[3]); 
     } 
    } 

} 

回答

0

我找到了解決辦法。

更改它

 if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) { 
       ... 
       return true; 
      } 
      return false; 

要:

if (DrawableClick.isDrawableClick(motionEvent, et3, DRAWABLE_RIGHT)) { 
      ... 
      motionEvent.setAction(MotionEvent.ACTION_CANCEL); 
     } 
     return false;