Java Android上的密码生成器
我刚刚开始创建android应用程序,第一个想法是“为android创建随机密码生成器,只是为了练习” 是的,但那就像 布局如下:Java Android上的密码生成器,java,android,Java,Android,我刚刚开始创建android应用程序,第一个想法是“为android创建随机密码生成器,只是为了练习” 是的,但那就像 布局如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" androi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ru.hashmap.myapplol.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Your password should be here"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:id="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:singleLine="true" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/progressBar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:indeterminate="true" />
<Button
android:text="generate!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="62dp"
android:id="@+id/button"
android:layout_below="@+id/progressBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="genPass"/>
<SeekBar
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="15"
android:progress="6"
android:layout_marginTop="27dp"
android:id="@+id/seekBar"
android:layout_below="@+id/progressBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
代码如下:
package ru.hashmap.myapplol;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void genPass(View view)
{
SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
int seekValue = seek.getProgress()+1;
Log.d("seekValue", seekValue+"");
String[] dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
String[] pass = new String[seekValue];
Random rnd = new Random();
for(int i = 0; i < seekValue; i++) {
pass[i] = dict[rnd.nextInt(dict.toString().length())];
}
((TextView) findViewById(R.id.editText)).setText(pass.toString());
Log.d("pass", pass+"");
}
}
包ru.hashmap.myapplol;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.SeekBar;
导入android.widget.TextView;
导入java.util.Random;
公共类MainActivity扩展了AppCompatActivity{
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
公共通行证(查看)
{
SeekBar seek=(SeekBar)findViewById(R.id.SeekBar);
int seekValue=seek.getProgress()+1;
d(“seekValue”,seekValue+”);
字符串[]dict=“0123456789abcdefghijklmnopqrstuvwxyzabdfghijklmnopqrstuvwxyz”。拆分(“”);
String[]pass=新字符串[seekValue];
随机rnd=新随机();
for(int i=0;i
如何使其按预期工作?根本没有问题,您只是在这里打印数组的引用地址
setText(pass.toString())
,它看起来像[Ljava.lang.String;@52e922
您需要一个StringBuffer
或StringBuilder
来追加字符,然后将其显示为字符串
SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
int seekValue = seek.getProgress()+1;
Log.d("seekValue", seekValue+"");
String[] dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
Random rnd = new Random();
StringBuilder str= new StringBuilder();
for(int k = 0; k < seekValue; k++) {
str.append(dict[rnd.nextInt(dict.length)]);
}
// don't use this , will display reference of array
//((TextView) findViewById(R.id.editText)).setText(pass.toString());
((TextView) findViewById(R.id.editText)).setText(str.toString());
// use this and avoid array in this case
SeekBar seek=(SeekBar)findViewById(R.id.SeekBar);
int seekValue=seek.getProgress()+1;
d(“seekValue”,seekValue+”);
字符串[]dict=“0123456789abcdefghijklmnopqrstuvwxyzabdfghijklmnopqrstuvwxyz”。拆分(“”);
随机rnd=新随机();
StringBuilder str=新的StringBuilder();
for(int k=0;k
dict.toString再次为您提供参考地址,该地址的长度与
[Ljava.lang.String;@52e922
表示此dict.toString().length()
始终给出26左右的长度,因此您的密码将包含字符始终在此限制附近,以便获得dict
数组的实际长度,即62,使用dict.length
根本没有问题,您只需在此处打印数组的参考地址setText(pass.toString())
看起来像什么东西[Ljava.lang.String;@52e922
您需要一个StringBuffer
或StringBuilder
来追加字符,然后将其显示为字符串
SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
int seekValue = seek.getProgress()+1;
Log.d("seekValue", seekValue+"");
String[] dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
Random rnd = new Random();
StringBuilder str= new StringBuilder();
for(int k = 0; k < seekValue; k++) {
str.append(dict[rnd.nextInt(dict.length)]);
}
// don't use this , will display reference of array
//((TextView) findViewById(R.id.editText)).setText(pass.toString());
((TextView) findViewById(R.id.editText)).setText(str.toString());
// use this and avoid array in this case
SeekBar seek=(SeekBar)findViewById(R.id.SeekBar);
int seekValue=seek.getProgress()+1;
d(“seekValue”,seekValue+”);
字符串[]dict=“0123456789abcdefghijklmnopqrstuvwxyzabdfghijklmnopqrstuvwxyz”。拆分(“”);
随机rnd=新随机();
StringBuilder str=新的StringBuilder();
for(int k=0;k
dict.toString再次为您提供参考地址,该地址的长度与
[Ljava.lang.String;@52e922
表示此dict.toString().length()
始终给出26左右的长度,这样您的密码将包含字符始终在此限制附近,以便获得dict
数组的实际长度,即62,请使用dict.length
您已将pass
定义为数组,但请尝试在标量上下文中使用它:
String[] pass = new String[seekValue];
...
((TextView) findViewById(R.id.editText)).setText(pass.toString());
在这种情况下,toString()
将转储数组,包括元素类型和起始内存地址
您需要处理该数组的单个元素,如
((TextView) findViewById(R.id.editText)).setText(pass[i].toString());
但我猜不出如何确定正确的索引值。您已将
传递定义为数组,但请尝试在标量上下文中使用它:
String[] pass = new String[seekValue];
...
((TextView) findViewById(R.id.editText)).setText(pass.toString());
在这种情况下,toString()
将转储数组,包括元素类型和起始内存地址
您需要处理该数组的单个元素,如
((TextView) findViewById(R.id.editText)).setText(pass[i].toString());
但是我猜不出如何确定正确的索引值。打印stacktrace/log或输出首先,你似乎没有在任何地方调用genPass方法。为什么你有progress bar和seekbar?@helldawg13ProgressBar
就像html
标记,我懒得搜索任何其他路径。genPass
是从android:onClick方法调用的。如果我理解正确,你想使用seekbar生成随机密码来证明其长度正确吗?打印你的stacktrace/log或输出首先,你似乎没有在任何地方调用genPass方法。为什么你有进度条和seekbar?@helldawg13ProgressBar
就像html
标记一样,我太懒了,无法搜索任何其他路径。genPass
是从android:onClick
方法调用的,因此如果我正确理解,您希望使用seekbar生成随机密码来证明长度?但是密码没有大写和小写的部分ers@HashMap这是同一个问题,请尝试更新答案并实施,公关