English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Guardiamo prima l'effetto visivo
Pensiero di implementazione:
Il componente che diventa un punto non è TextView né EditText, ma Imageview. Prima di tutto, scrivi un RelativeLayout che contiene 6 ImageView e un EditText (l'EditText deve coprire l'ImageView) e imposta lo sfondo dell'EditText come trasparente.
<?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>
Creare un controllo personalizzato ItemPasswordLayout per eseguire alcune operazioni di layout, con un focus speciale sulla rimozione del cursore di EditText e sull'ascolto degli eventi di testo inserito. Dopo la modifica del testo, il testo viene messo in un StringBuffer e l'edittext viene impostato su "". Inoltre, viene ascoltato l'evento di pressione della tastiera di cancellazione, e quando viene premuto il tasto cancellazione, il carattere corrispondente nel StringBuffer viene eliminato.
/** * Layout del controllo di input della password * Creato da Went_Gone il 14/9/2016. */ public class ItemPasswordLayout extends RelativeLayout{ private EditText editText; private ImageView[] imageViews; // array di ImageView per memorizzare i riquadri della password private StringBuffer stringBuffer = new StringBuffer(); // buffer di stringa per memorizzare i caratteri della password private int count = 6; private String strPassword; // stringa della password 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); // nascondi il cursore 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位时edittext置空 editText.setText(""); return; }else { //将文字添加到StringBuffer中 stringBuffer.append(editable); editText.setText("");//添加后将EditText置空 造成没有文字输入的错局 Log.e("TAG", "afterTextChanged: stringBuffer è "+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){ //Eliminare il carattere alla posizione corrispondente stringBuffer.delete((count-1),count); count--; Log.e("TAG", "afterTextChanged: stringBuffer è "+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; } /** * Ottenere la password * @return */ public String getStrPassword() { return strPassword; } public void setContent(String content){ editText.setText(content); } }
Poi è sufficiente chiamare Activity.
in xml dichiarare
<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>
Chiamata in Activity
itemPasswordLayout = (ItemPasswordLayout) findViewById(R.id.act_zhifubao_IPLayout); itemPasswordLayout.setInputCompleteListener(new ItemPasswordLayout.InputCompleteListener() { @Override public void inputComplete() { Toast.makeText(ZhifubaoActivity.this, "La password è: "+itemPasswordLayout.getStrPassword(), Toast.LENGTH_SHORT).show(); } });
Conclusione
Bene, il contenuto di questo articolo è giunto a fine, è così semplice. Spero che questo articolo possa aiutare tutti a imparare o lavorare. Se hai domande, puoi lasciare un messaggio per discuterne.
Dichiarazione: il contenuto di questo articolo è stato raccolto da Internet, il copyright spetta agli autori originali, il contenuto è stato caricato autonomamente dagli utenti di Internet, questo sito non detiene il diritto di proprietà, non è stato editato manualmente e non assume responsabilità per le relative responsabilità legali. Se trovi contenuti sospetti di violazione del copyright, ti preghiamo di inviare una e-mail a: notice#oldtoolbag.com (al momento dell'invio dell'e-mail, sostituisci # con @) per segnalare e fornire prove pertinenti. Una volta verificata, questo sito rimuoverà immediatamente il contenuto sospetto di violazione del copyright.