Android 삭제 기능 이 있 는 입력 상자 컨트롤
효과 도 는 다음 과 같다.
 주요 한 사고방식 은 오른쪽 그림 에 감청 을 설정 하고 오른쪽 그림 을 클릭 하여 입력 상자 의 내용 을 지우 고 삭제 아이콘 을 숨 기 는 것 이다.왜냐하면 우 리 는 EditText 에 클릭 이 벤트 를 직접 설정 할 수 없 기 때문에 우 리 는 우리 가 누 른 위 치 를 기억 해서 클릭 이 벤트 를 모 의 하고 입력 상자 의 onTouchEvent()방법 으로 모 의 한다.
package com.xiaolijuan.edittextwithdeldome;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;
/**
 * @author: adan
 * @description:           EditText
 * @projectName: EditTextWithDelDome
 * @date: 2016-02-28
 * @time: 23:34
 */
public class EditTextWithDel extends EditText {
 private final static String TAG = "EditTextWithDel";
 private Drawable imgInable;
 private Drawable imgAble;
 private Context mContext;
 public EditTextWithDel(Context context) {
 super(context);
 mContext = context;
 init();
 }
 public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 mContext = context;
 init();
 }
 public EditTextWithDel(Context context, AttributeSet attrs) {
 super(context, attrs);
 mContext = context;
 init();
 }
 private void init() {
 imgAble = mContext.getResources().getDrawable(
  R.mipmap.icon_delete_gray);
 addTextChangedListener(new TextWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before,
     int count) {
  }
  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
      int after) {
  }
  @Override
  public void afterTextChanged(Editable s) {
  setDrawable();
  }
 });
 setDrawable();
 }
 //       
 private void setDrawable() {
 if (length() < 1) {
  setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
 } else {
  setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
 }
 }
 //       
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
  int eventX = (int) event.getRawX();
  int eventY = (int) event.getRawY();
  Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
  Rect rect = new Rect();
  getGlobalVisibleRect(rect);
  rect.left = rect.right - 50;
  if (rect.contains(eventX, eventY))
  setText("");
 }
 return super.onTouchEvent(event);
 }
 @Override
 protected void finalize() throws Throwable {
 super.finalize();
 }
}
setDrawable()방법,setCompound Drawables With IntrinsicBounds(Drawable left,Drawable top,Drawable right,Drawable bottom)는 위,아래,왼쪽,오른쪽 에 아이콘 을 설정 하고 어 딘 가 에 표시 하지 않 으 려 면 null 로 설정 합 니 다.다음은 Activity 의 레이아웃 을 설정 합 니 다.사용자 정의 입력 상자,단추
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="60dp"
 android:layout_margin="25dp"
 android:background="#ffffff">
 <ImageView
  android:id="@+id/img"
  android:layout_width="25dp"
  android:layout_height="30dp"
  android:layout_alignParentLeft="true"
  android:layout_centerVertical="true"
  android:layout_margin="5dp"
  android:src="@mipmap/ic_launcher" />
 <ImageView
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:layout_alignParentBottom="true"
  android:layout_marginLeft="2dp"
  android:layout_marginRight="2dp"
  android:background="#56AB55" />
 <com.xiaolijuan.edittextwithdeldome.EditTextWithDel
  android:id="@+id/et_phoneNumber"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:layout_margin="2dp"
  android:layout_toRightOf="@+id/img"
  android:background="#ffffff"
  android:hint="       "
  android:maxLength="11"
  android:phoneNumber="true"
  android:singleLine="true" />
 </RelativeLayout>
 <Button
 android:id="@+id/btn"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="25dp"
 android:background="#56AB55"
 android:text="  " />
</LinearLayout>
그 다음 에 인터페이스 코드 의 작성 입 니 다.주로 입력 상 자 를 테스트 합 니 다.
package com.xiaolijuan.edittextwithdeldome;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
 private EditText userName;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 userName = (EditText) findViewById(R.id.et_phoneNumber);
 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if (TextUtils.isEmpty(userName.getText().toString())){
   Toast.makeText(getApplicationContext(),"      ",Toast.LENGTH_LONG).show();
   return;
  }
  Toast.makeText(getApplicationContext(),userName.getText().toString(),Toast.LENGTH_LONG).show();
  }
 });
 }
}
원본 다운로드:http://xiazai.jb51.net/201609/yuanma/EditTextWithDel(jb51.net).rar이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.