[英]Android Custom Lock Screen Password
I'm creating a lock screen app for android, but I'm having trouble getting the activity to finish when the correct password is entered. 我正在为Android创建锁屏应用程序,但是在输入正确的密码时无法完成活动。 The code works only works if the password is one character;
该代码仅在密码为一个字符时有效。 otherwise, it doesn't unlock.
否则,它不会解锁。 I think it has to do with how I'm modifying the global variables.
我认为这与我修改全局变量的方式有关。
This is the code I'm using to test. 这是我用来测试的代码。 It should unlock with by touching the two textviews in order, but doesn't.
它应该通过依次触摸两个文本视图来解锁,但不可以。
public class LockScreen extends Activity implements OnClickListener {
String password = "cd";
String guess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lock_layout);
TextView c = (TextView) findViewById(R.id.c);
TextView d = (TextView) findViewById(R.id.d);
c.setOnClickListener(this);
d.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.c:
if (guess == null) {
guess = "c";
} else {
guess += "c";
}
break;
case R.id.d:
if (guess == null) {
guess = "d";
} else {
guess += "d";
}
break;
}
if (guess == password) {
finish();
}
}
- Use equals()
method to compare. -使用
equals()
方法进行比较。
- In Java Objects
are compared using equals()
method, and String
is an object, so its should also follow the same trend as object. -在Java中,使用
equals()
方法比较Objects
,并且String
是对象,因此它也应遵循与对象相同的趋势。
- ==
are used to see if 2 or more Object Reference Variables
are pointing on the same object or not on the heap. -
==
用于查看是否有2个或更多Object Reference Variables
指向同一对象或不在堆上。
- And just a piece of advice, that its always better to use char[]
(ie char array) for storing password instead of String
. -还有一点建议,使用
char[]
(即char数组)存储密码而不是String
总是更好。
Eg: 例如:
if (guess.equals(password)){
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.