Android 사용자 정의 EditText 타 오 바 오 로그 인 기능 구현
우선 효과 그림 보기:
전체 레이아웃 UI:
<com.example.zdyedittext.ClearEditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/imageView1"
android:background="@android:color/white"
android:ems="10"
android:hint=" "
android:padding="8dp"
android:singleLine="true" />
<com.example.zdyedittext.ClearEditText
android:id="@+id/et_pass_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" "
android:background="@android:color/white"
android:password="true"
android:padding="8dp"
android:singleLine="true" />
사용자 정의 EditText 클래스사용자 정의 EditText 때문에 당연히 EditText 를 통합 해 야 합 니 다.
public class ClearEditText extends EditText
그리고 구조 방법 을 추가 하 는 것 은 XML 에서 인용 할 수 있 도록 하 는 것 이다.
public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}
다음은 자신의 EditText 스타일 을 설정 하고 원 하 는 스타일 을 추가 하 는 것 입 니 다.구체 적 으로 init()방법 에서 이 루어 집 니 다.
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
init()방법의 실현 과정:[2]매개 변 수 는 dr.mDrawable Right 이 고 삭제 단 추 는 EditText 오른쪽 에 있 으 며 아이콘 의 왼쪽 위 와 오른쪽 아래 를 설정 합 니 다.mClearDrawable.setBounds(0,0,mClearDrawable.getIntrinsic Width(),mClearDrawable.getIntrinsic Height();
private void init() {
// EditText DrawableRight,
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
mClearDrawable = getResources().getDrawable(R.drawable.del);//R.drawable.del
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
//
//
setClearIconVisible(false);
//
setOnFocusChangeListener(this);
//
addTextChangedListener(this);
}
EditText 에 감청 이 벤트 를 직접 설정 할 수 없 기 때문에 클릭 위 치 를 기록 하여 클릭 이 벤트 를 모 의 하고 물고기 아이콘 의 좌우 클릭 만 기록 합 니 다.
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));
if (touchable) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
입력 상자 에 텍스트 가 있 는 지,동적 설정 삭제 아이콘 의 표시 와 숨 김 을 판단 합 니 다.
public void onFocusChange(View v, boolean hasFocus) {
this.hasFoucs = hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
}
입력 상자 에 텍스트 가 오래 있 으 면 삭제 아이콘 을 그립 니 다.
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
입력 상자 의 내용 이 바 뀌 었 을 때 아이콘 을 동적 으로 바 꾸 어 삭제 합 니 다.
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFoucs) {
setClearIconVisible(s.length() > 0);
}
}
이로써 완성 되 었 습 니 다.상자 에 텍스트 가 없 을 때 아이콘 을 삭제 하고 텍스트 입력 이 있 을 때 아이콘 표 시 를 삭제 하고 아이콘 삭 제 를 클릭 하여 텍스트 내용 을 비 웁 니 다.사용자 정의 InputType 을"*"로 되 돌려 줍 니 다.
암호 스타일 을 설정 하면 PasswordTransformationMethod 와 같은 종 류 를 계승 하고 CharSequence 방법 을 실현 하여 CharAt 의 반환 값 을'*'로 수정 하면 됩 니 다.
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
InputType
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
그리고 메 인 프로그램 에서 컨트롤 을 초기 화하 고 레이아웃 에 android:password="true"줄 코드 를 설정 하여 코드 에 암호 입력 의 반환 스타일 을 동적 으로 설정 합 니 다.
et_pass_word = (ClearEditText) findViewById(R.id.et_pass_word);
et_pass_word.setTransformationMethod(new EditTextBgToStar());
요약:사용자 정의 EditText 에 아이콘 을 삭제 하 는 감청 을 추가 합 니 다.직접 설정 할 수 없 기 때문에 누 른 위 치 를 기록 하여 클릭 이 벤트 를 모 의 합 니 다.전체적인 실현 방향 은 EditText 오른쪽 에 삭제 아이콘 을 그 린 다음 에 동적 설정 으로 표시 하거나 숨 기 는 것 입 니 다.감청 이 벤트 를 설정 하여 동적 으로 표시 하고 텍스트 상자 의 텍스트 를 제거 하 는 것 입 니 다.사용자 정의 암호 가 스타일 로 돌아 갈 때 주로 Password TransformationMethod 와 같은 종 류 를 계승 하여 CharSequence 방법 을 실현 하고 입력 스타일 을 사용자 정의 로 바 꿉 니 다.
클릭원본 코드 다운로드
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.