Android 에서 EditText 의 drawableRight 속성 설정 클릭 이벤트
2458 단어 EditTextdrawableRight클릭 이벤트
Google 공식 API 는 오른쪽 그림 의 클릭 이 벤트 를 설정 하 는 직접적인 방법 을 제시 하지 않 았 습 니 다.따라서 클릭 위 치 를 통 해 클릭 이 벤트 를 판단 해 야 합 니 다.
레이아웃 파일:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.drablerighttest.MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/search_clear_pressed"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) this.findViewById(R.id.et);
et.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// et.getCompoundDrawables() 4 ,
Drawable drawable = et.getCompoundDrawables()[2];
// ,
if (drawable == null)
return false;
// ,
if (event.getAction() != MotionEvent.ACTION_UP)
return false;
if (event.getX() > et.getWidth()
- et.getPaddingRight()
- drawable.getIntrinsicWidth()){
et.setText("");
}
return false;
}
});
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
EditText 컨텐트 끝내기WebView처럼 EditText 내에서 텍스트를 읽어들이고 싶어서 조사했습니다. 구체적으로 말하자면, 나는 편집 중인 텍스트를 검색하는 기능을 만들고 싶은 동기가 있다. EditText에서find의 API를 준비하...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.