2012-09-25 2 views
7

텍스트 뷰 위에 십자 버튼을 놓는 데 문제가 있습니다. LinearLayout을 사용하고 있는데, Framelayout에서 작동하지만, 그게 내 목적을 해결하지는 않지만, 그걸로 올라 오지는 않습니다. 참조 용으로 XML을 첨부하고 있습니다.이 문제를 해결하는 데 도움을주십시오.Android : 자동 완성 텍스트 위에 십자가 아이콘을 붙이는 방법

<LinearLayout 
     android:id="@+id/top" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/phone_toolbar" 
     android:baselineAligned="true" 
     android:gravity="center_horizontal" 
     android:paddingBottom="2dp" 
     android:paddingTop="2dp" > 

     <ImageView 
      android:id="@+id/search_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="10dp" 
      android:background="@drawable/toolbar_search_icon_phone" > 
     </ImageView> 

     <AutoCompleteTextView 
      android:id="@+id/text" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="10dp" 
      android:layout_weight="2" 
      android:background="@drawable/toolbar_phone_textfield" 
      android:dropDownVerticalOffset="5dp" 
      android:ems="10" 
      android:focusable="true" 
      android:hint="@string/hint" 
      android:imeOptions="actionSearch" 
      android:paddingLeft="10dp" 
      android:paddingRight="20dp" 
      android:singleLine="true" 
      android:textColor="#000000" 
      android:textSize="14sp" /> 

     <Button 
      android:id="@+id/clear_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right|center_vertical" 
      android:layout_marginRight="10dip" 
      android:background="@drawable/text_clear" /> 
    </LinearLayout> 

고마워요!

답변

26

EditText에서 android : drawableLeft 속성을 사용하십시오. 사용자 정의 글고 치기를 사용

String value = "";//any text you are pre-filling in the EditText 

final EditText et = new EditText(this); 
et.setText(value); 
final Drawable x = getResources().getDrawable(R.drawable.presence_offline);//your x image, this one from standard android images looks pretty good actually 
x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight()); 
et.setCompoundDrawables(null, null, value.equals("") ? null : x, null); 
et.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (et.getCompoundDrawables()[2] == null) { 
      return false; 
     } 
     if (event.getAction() != MotionEvent.ACTION_UP) { 
      return false; 
     } 
     if (event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth()) { 
      et.setText(""); 
      et.setCompoundDrawables(null, null, null, null); 
     } 
     return false; 
    } 
}); 
et.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     et.setCompoundDrawables(null, null, et.getText().toString().equals("") ? null : x, null); 
    } 

    @Override 
    public void afterTextChanged(Editable arg0) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 
}); 

이도 수행 할 수 있습니다 클릭 이벤트를 처리하기 위해

EditText et = (EditText) findViewById(R.id.myET); 
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_icon, 0, 0, 0); 

를 : 동적 아이콘을 추가 할 경우

<EditText 
...  
android:drawableLeft="@drawable/my_icon" /> 

, 이것을 사용 :

Handling click events on a drawable within an EditText

+0

고맙습니다.하지만 해당 드로어 블에서 이벤트 클릭을 어떻게 추적 할 수 있습니까? 자동 완성 텍스트 뷰에있는 텍스트를 삭제하고 싶습니다. – Anupam

+0

업데이트 된 질문과 함께 –

+0

링크도 확인하십시오. 코드에 문제가 있습니다. 그것은 et.setCompoundDrawables 대신에 setCompoundDrawablesWithIntrinsicBounds가되어야합니다. –

관련 문제