2014-08-27 2 views
0

У меня FrameLayout, который состоит из EditText и ImageButton и выглядит следующим образом:Android EditText и ImageButton предотвратить накладывания

image

В коде это выглядит следующим образом:

<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/input_field_red" 
      android:hint="Message" 
      android:singleLine="true" 
      android:textColor="@color/dark_grey_3" /> 

     <ImageButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:background="@android:color/transparent" 
      android:src="@drawable/cancel" /> 
    </FrameLayout> 

Проблема есть, когда текст ввода становится достаточно длинным, ImageButton накладывает текст.

Есть ли способ предотвратить это? Я не должен сокращать длину линии. Это должно быть match_parent. И ImageButton должен быть расположен на линии и выровнен по правому краю.

+0

См http://stackoverflow.com/questions/6355096/how-to -create-edittext-with-crossx-button-at-end-the- –

+0

самый передовой пост не решает мою проблему. Он использует почти те же атрибуты, что и я. – user3199693

+0

Пожалуйста, взгляните на https://github.com/yanchenko/droidparts/blob/master/droidparts/src/org/droidparts/widget/ClearableEditText.java, возможно, это может помочь вам –

ответ

-2

ли что-то подобное -

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:id="@+id/EditViewId" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/input_field_red" 
     android:hint="Message" 
     android:layout_toLeftOf="@+id/imageViewId" 
     android:singleLine="true" 
     android:textColor="@color/dark_grey_3" /> 

    <ImageButton 
     android:id="@+id/imageViewId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/EditViewId" 
     android:layout_alignLeft="@+id/EditViewId" 
     android:layout_gravity="right" 
     android:background="@android:color/transparent" 
     android:src="@drawable/cancel" /> 
</RelativeLayout> 

Это предотвратить его overlapting

Другой метод
Существует еще один метод, чтобы добавить изображение в правом EditText.

<EditText 
     android:id="@+id/search" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_margin="4dip" 
     android:layout_weight="1" 
     android:background="@drawable/textfield_search1" 
     android:drawableRight="@drawable/search_icon" 
     android:hint="Search Anything..." 
     android:padding="4dip" 
     android:singleLine="true" /> 

и установка onClickListener для изображения на правой

Скажем, у меня есть EditText editComment с drawableRight

editComment.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      final int DRAWABLE_LEFT = 0; 
      final int DRAWABLE_TOP = 1; 
      final int DRAWABLE_RIGHT = 2; 
      final int DRAWABLE_BOTTOM = 3; 

      if(event.getAction() == MotionEvent.ACTION_UP) { 
       if(event.getX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
        // your action here 
       } 
      } 
      return false; 
     } 
    }); 
+0

Вы пытались скомпилировать это как минимум? –

+0

не скомпилирован, но я уверен, что это сработает. Просто попробуйте , если нет, попробуйте еще раз, я отредактировал. –

+0

ОК, только глядя на него Я могу назвать четыре ошибки: 1) нет андроида: атрибут toLeftOf; 2) «@ id/imageViewId» следует использовать в атрибуте android: layout_toLeftOf, а не «@ + id/imageViewId»; 3) Элемент с данным идентификатором должен быть определен перед элементом, который использует android: layout_toLeftOf. 4) android: layout_toLeftOf применим к RelativeLayout, а не FrameLayout –