English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
이미지를 보겠습니다
구현 방법:
점으로 변하는 컨트롤은 TextView와 EditText이 아니라 ImageView입니다. 먼저 RelativeLayout을 작성하여6ImageView와 EditText(EditText은 ImageView를 덮어야 합니다)를 사용하여 EditText의 배경을 투명으로 설정합니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:background="@android:color/white"> <ImageView android:id="@"+id/item_password_iv1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@"+id/item_password_iv2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@"+id/item_password_iv3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@"+id/item_password_iv4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@"+id/item_password_iv5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> <ImageView android:id="@"+id/item_password_iv6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/nopassword"/> </LinearLayout> <EditText android:id="@"+id/item_edittext" android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/transparent"/> </RelativeLayout>
컨트롤 ItemPasswordLayout를 정의하여 레이아웃에 처리를 추가합니다. 주요 목표는 EditText의 커서를 제거하고 입력 텍스트 이벤트를 감지하여 텍스트 변화 시 StringBuffer에 텍스트를 저장하고 editText를 ""로 설정하는 것입니다. 또한 키보드 지우기 키 이벤트를 감지하여 지우기 키를 누를 때 StringBuffer에서 해당 위치의 문자를 제거합니다.
/** * 패스워드 입력 박스 컨트롤러 레이아웃 * Created by Went_Gone on 2016/9/14. */ public class ItemPasswordLayout extends RelativeLayout{ private EditText editText; private ImageView[] imageViews;//패스워드 박스를 저장하는 배열 사용 private StringBuffer stringBuffer = new StringBuffer();//패스워드 문자를 저장 private int count = 6; private String strPassword;//패스워드 문자열 public ItemPasswordLayout(Context context) { this(context,null); } public ItemPasswordLayout(Context context, AttributeSet attrs) { this(context, attrs,0); } public ItemPasswordLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); imageViews = new ImageView[6]; View view = View.inflate(context, R.layout.item_password,this); editText = (EditText) findViewById(R.id.item_edittext); imageViews[0] = (ImageView) findViewById(R.id.item_password_iv1); imageViews[1] = (ImageView) findViewById(R.id.item_password_iv2); imageViews[2] = (ImageView) findViewById(R.id.item_password_iv3); imageViews[3] = (ImageView) findViewById(R.id.item_password_iv4); imageViews[4] = (ImageView) findViewById(R.id.item_password_iv5); imageViews[5] = (ImageView) findViewById(R.id.item_password_iv6); editText.setCursorVisible(false);//커서를 숨기기 setListener(); } private void setListener() { editText.addTextChangedListener(new TextWatcher() {}}) @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2}) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2}) { } @Override public void afterTextChanged(Editable editable) { //중요: 문자가 ""가 아니면 작업을 수행합니다 if (!editable.toString().equals("")) { if (stringBuffer.length()>5){ //비밀번호 길이가 더 크다면5문자열 길이가 0일 때 editText를 비우습니다 editText.setText(""); return; }else { //문자를 StringBuffer에 추가합니다 stringBuffer.append(editable); editText.setText("");//추가 후 EditText를 비우면 입력이 없는 오류가 발생합니다 Log.e("TAG", "afterTextChanged: stringBuffer is "+stringBuffer); count = stringBuffer.length();//stringbuffer의 길이를 기록합니다 strPassword = stringBuffer.toString(); if (stringBuffer.length()==6){ //문자열 길이는6 이를 통해 입력 완료를 감지합니다 if (inputCompleteListener!=null){ inputCompleteListener.inputComplete(); } } } for (int i =0;i<stringBuffer.length();i++){ imageViews[i].setImageResource(R.mipmap.ispassword); } } } }); editText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { // Log.e("TAG", "afterTextChanged: stringBuffer is "+stringBuffer); if (onKeyDelete()) return true; return true; } return false; } }); } public boolean onKeyDelete() { if (count==0){ count = 6; return true; } if (stringBuffer.length()>0){ //해당 위치의 문자를 삭제합니다 stringBuffer.delete((count-1),count); count--; Log.e("TAG", "afterTextChanged: stringBuffer is "+stringBuffer); strPassword = stringBuffer.toString(); imageViews[stringBuffer.length()].setImageResource(R.mipmap.nopassword); } return false; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } private InputCompleteListener inputCompleteListener; public void setInputCompleteListener(InputCompleteListener inputCompleteListener) { this.inputCompleteListener = inputCompleteListener; } public interface InputCompleteListener{ void inputComplete(); } public EditText getEditText() { return editText; } /** * 비밀번호를 가져오기 * @return */ public String getStrPassword() { return strPassword; } public void setContent(String content){ editText.setText(content); } }
그래서 Activity에서 호출만 해야 합니다.
xml에 선언됩니다.
<com.example.went_gone.demo.view.ItemPasswordLayout> android:id="@"+id/act_zhifubao_IPLayout android:layout_width="match_parent" android:layout_height="wrap_content"> </com.example.went_gone.demo.view.ItemPasswordLayout>
Activity에서 호출
itemPasswordLayout = (ItemPasswordLayout) findViewById(R.id.act_zhifubao_IPLayout); itemPasswordLayout.setInputCompleteListener(new ItemPasswordLayout.InputCompleteListener() { @Override public void inputComplete() { Toast.makeText(ZhifubaoActivity.this, "비밀번호는:"+itemPasswordLayout.getStrPassword(), Toast.LENGTH_SHORT).show(); } });
정리
좋습니다. 본문의 내용은 여기까지입니다. 이렇게만 되면 됩니다. 간단하지 않습니까? 이 기사가 여러분의 학습이나 업무에 도움이 되길 바랍니다. 의문이 있으시면 댓글을 통해 교류해 주세요.
선언: 본문 내용은 인터넷에서 가져왔으며, 저작권자는 본인입니다. 내용은 인터넷 사용자가 자발적으로 기여하고 자체로 업로드한 것이며, 본 사이트는 소유권을 가지지 않으며, 인공 편집 처리를 하지 않았으며, 관련 법적 책임도 부담하지 않습니다. 저작권 위반이 의심되는 내용이 있으시면, 메일을 보내 주시기 바랍니다: notice#oldtoolbag.com(보고할 때는 #을 @으로 변경하십시오.)를 통해 신고하시고 관련 증거를 제공하시면, 사실이 확인되면 해당 사이트는 즉시 위반 내용을 삭제합니다.