Password Generator Error (Android) -
i dont know why cant generate custom password.
public void generatepass(view view) { edittext textfield = (edittext)findviewbyid(r.id.digitsfield); int digitsnum = integer.parseint(textfield.gettext().tostring()); if (digitsnum != 1){ random pass = new random(); int num1 = pass.nextint(10); textview passtext = (textview) findviewbyid(r.id.passtext); passtext.settext(num1); } }
i created text field(edittext) in can write how many digits want password be, tried putting
edittext textfield = (edittext)findviewbyid(r.id.digitsfield); string digits = textfield.gettext().tostring(); int digitsnum = integer.parseint(num);
and changing if(digitsnum == 1)... when write digits(i code 1) tried writing 1 crash or stop working.
try , modify needs:
java code (add in oncreate method):
final textview passtext = (textview) findviewbyid(r.id.passtext); final edittext digitsfield = (edittext) findviewbyid(r.id.digitsfield); button generatepassword = (button) findviewbyid(r.id.generatepassword); generatepassword.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int passwordlength = integer.parseint(digitsfield.gettext().tostring()); string allowedchars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; char[] allowedcharsarray = allowedchars.tochararray(); char[] chars = new char[passwordlength]; random random = new random(); (int = 0; < passwordlength; i++) { chars[i] = allowedcharsarray[random.nextint(allowedchars.length())]; } passtext.settext(chars, 0, passwordlength); } });
xml layout (edit current layout):
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="password" android:layout_gravity="center" android:id="@+id/passtext" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="number" android:layout_margintop="20dp" android:layout_marginbottom="20dp" android:id="@+id/digitsfield" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="generate password" android:layout_gravity="center" android:id="@+id/generatepassword" /> </linearlayout>
Comments
Post a Comment