How to create a four-digit password.

I want to create a layout for an Android app that has a numeric keypad and takes four digits to determine if it matches the given password value.

I saw several applications use this, so I would think that this is a high level widget of some description.

The only thing I can find that is remotely close to what I want is this:

<EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="numberPassword" > <requestFocus /> </EditText> 

But this is not quite what I am looking for.

Any entry will be awesome and in advance.

EDIT: here is an image of the initial screen of an iOS application application that I would like to

Dropbox iOS passcode screen

+6
source share
4 answers

In the end, I created some custom widgets.

There is a keyboard widget. It inherits from TableLayout and has four rows of three buttons. Each button changes the line - either adds numbers or deletes them.

Another widget I added was called PINEntry. It takes one int to indicate how many digits have been entered, and displays this information accordingly.

I use these two widgets together in a different view to recreate the access code screen.

0
source

You tried to add this:

 android:maxLength="4" android:password="true" 

This leads to a more password, for example.

Update: I would do four EditText and make them each maxLength = "1". If you align them horizontally, this should work :)

+4
source

I am new to Android. when I'm stuck in coding, I always use stackoverflow for reference. I learned a lot from stackoverflow. This is the first time I dared to answer this question. Forgive me if I am mistaken, and any suggestion regarding coding or a way to write code in stackoverflow is much appreciated. Thanks..

I did something similar in Fragments. Take 4 EditText and set the maxLength attribute to 1 in xml for all 4 EditTexts. You can change the EditText to suit your requirement.

Note. The OnKey method may or may not be called for DEL (BackSpace) on the Android Android keyboard.

 public class VerifyCodeFrag extends Fragment implements TextWatcher,View.OnKeyListener,View.OnFocusChangeListener { private EditText et_digit1, et_digit2, et_digit3, et_digit4;//In this et_digit1 is Most significant digit and et_digit4 is least significant digit private int whoHasFocus; char[] code = new char[4];//Store the digits in charArray. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_verify_code, container, false); initializeView(view); et_digit1.requestFocus();//Left digit gets focus after adding of fragment in Container return view; } 

This method is used for intuition of representations.

 private void initializeView(View view) { et_digit1 = (EditText) view.findViewById(R.id.et_vfcode_digit1); et_digit2 = (EditText) view.findViewById(R.id.et_vfcode_digit2); et_digit3 = (EditText) view.findViewById(R.id.et_vfcode_digit3); et_digit4 = (EditText) view.findViewById(R.id.et_vfcode_digit4); setListners(); } 

This method is designed to set listeners for each EditTexts.

 private void setListners() { et_digit1.addTextChangedListener(this); et_digit2.addTextChangedListener(this); et_digit3.addTextChangedListener(this); et_digit4.addTextChangedListener(this); et_digit1.setOnKeyListener(this); et_digit2.setOnKeyListener(this); et_digit3.setOnKeyListener(this); et_digit4.setOnKeyListener(this); et_digit1.setOnFocusChangeListener(this); et_digit2.setOnFocusChangeListener(this); et_digit3.setOnFocusChangeListener(this); et_digit4.setOnFocusChangeListener(this); } 

This is the OnFocusChangeListner interface override method, with which I check which EditText is currently focused on, where it is useful to extract the number from the corresponding EditText blocks in the afterTextChnged method (TextWatcher override method).

  @Override public void onFocusChange(View v, boolean hasFocus) { switch(v.getId()) { case R.id.et_vfcode_digit1: whoHasFocus=1; break; case R.id.et_vfcode_digit2: whoHasFocus=2; break; case R.id.et_vfcode_digit3: whoHasFocus=3; break; case R.id.et_vfcode_digit4: whoHasFocus=4; break; default: break; } } 

This is a method to override the TextWatcher interface. Here in this afterTextChanged (override) method I get a number from EdiTexts by storing them in the corresponding charArray index. And as soon as the user enters a number in EditText, the next EditText will receive focus using the requestfocus method (Example: et_digit2.requestFocus ()).

 @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { switch (whoHasFocus) { case 1: if(!et_digit1.getText().toString().isEmpty()) { code[0]= et_digit1.getText().toString().charAt(0); et_digit2.requestFocus(); } break; case 2: if(!et_digit2.getText().toString().isEmpty()) { code[1]= et_digit2.getText().toString().charAt(0); et_digit3.requestFocus(); } break; case 3: if(!et_digit3.getText().toString().isEmpty()) { code[2]= et_digit3.getText().toString().charAt(0); et_digit4.requestFocus(); } break; case 4: if(!et_digit4.getText().toString().isEmpty()) { code[3]= et_digit4.getText().toString().charAt(0); } break; default: break; } } 

This method will function as a delete key (BackSpace).
In this override method, I check if the EditText is empty and DEL (pressing the backspace key on the keyboard). if true, the previous EditText will get focus.

 @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_DEL) { switch(v.getId()) { case R.id.et_vfcode_digit2: if (et_digit2.getText().toString().isEmpty()) et_digit1.requestFocus(); break; case R.id.et_vfcode_digit3: if (et_digit3.getText().toString().isEmpty()) et_digit2.requestFocus(); break; case R.id.et_vfcode_digit4: if (et_digit4.getText().toString().isEmpty()) et_digit3.requestFocus(); break; default: break; } } } return false; } } 

Sample image.

[1]: https://i.stack.imgur.com/DAc9y.jpg

+3
source

I want to create a layout for an Android app with a numeric keypad and takes four digits to determine if it matches the pre-set password value.

Searching for a digital keypad of the client’s phone (for example, SipDroid ( dialpad link ), IMSDroid), restore the layout to your needs (for example, deleting #, * and other keys that you do not need).

use suggested attributes from @Tim Messerschmidt

Think of a safe way to store and retrieve this 4-digit pin code.

0
source

Source: https://habr.com/ru/post/918335/


All Articles