我正在嘗試制作一個用于學習的 android 應用程式,但我的按鈕只能點擊一次。我不能點擊第二次。這是我的按鈕代碼。
有xml代碼:
<Button
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:width="200dp"
android:height="80dp"
android:shadowColor="@color/red"
android:text="@string/button"
android:textColor="@color/black"
android:textSize="20sp"
android:visibility="visible"
app:cornerRadius="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/text"
app:rippleColor="@color/red"
app:strokeColor="@color/red"
tools:ignore="TextContrastCheck" />
還有java代碼:
buttonGenerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
passwordText.setText(generate());
}
});
有生成方法:
private String generate () {
Random random = new Random();
int character;
while (password.length() < 16) {
character = random.nextInt(76);
password = characters[character];
}
return password;
有變數:
private final String[] characters = {
"A" , "B" , "C" , "D" , "E" , "F" , "G" , "H",
"I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,
"U" , "V" , "W" , "X" , "Y" , "Z", "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h",
"i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" ,"u" , "v" , "w" , "x" , "y" , "z", "0" , "1" , "2" , "3" , "4" , "5" , "6" ,
"7" , "8" , "9", "!" , "#" , "'" , " " , "%" , "(" , ")" , "&" ,
"=" , "?" , "*" , "-" , "$" , "{" , "}" };
private String password = "";
uj5u.com熱心網友回復:
在第一次呼叫 generate() 之后,password.length() < 16將始終為 false。放入String passwordgenerate() 中:
private String generate () {
String password = "";
Random random = new Random();
int character;
while (password.length() < 16) {
character = random.nextInt(76);
password = characters[character];
}
return password;}
uj5u.com熱心網友回復:
似乎您的 generate() 函式在每次點擊時都會生成相同的字串,請更正您的 generate() 函式。
像這樣修改 generate() 函式以創建隨機密碼并傳遞您想要的密碼引數長度。
fun generate(length: Int) : String {
val allowedChars = ('A'..'Z') ('a'..'z') ('0'..'9')
return (1..length)
.map { allowedChars.random() }
.joinToString("")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/505024.html
上一篇:如何覆寫庫中的方法?
