简体   繁体   English

Android自定义锁屏密码

[英]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.

相关问题 在java中使用用户和密码锁定屏幕 - lock screen with user and password in java 通过主屏幕替换技术了解 Android 上的自定义锁定实现 - Understanding custom lock implementation on Android via home screen replacement technique 锁定屏幕上的Android通知 - Android notification on lock screen 自定义锁屏实现技术 - Custom Lock Screen Implementation Techniques Android以编程方式设置/更改/删除锁定屏幕PIN,密码或解锁图案 - Android to set/change/remove Lock Screen PIN, Password or Unlock Pattern programatically 如何以编程方式禁用屏幕锁定密码。 - How to disable screen lock password programmatically. 屏幕无法在Android中使用唤醒锁打开 - Screen not turning on with wake lock in Android 根据应用程序中的自定义事件在主屏幕/锁定屏幕上更改Android状态栏颜色 - Change Android status bar color on home/lock screen based on custom events within an app 自定义屏幕锁定,用于使用Android禁用后退按钮,主页按钮,设置按钮 - Custom screen lock for disable back button,home button,settings button using Android 需要工作解决方案在自定义应用程序中使用Android Pattern Lock Screen(而不是源代码重定向) - Need working solution to use Android Pattern Lock Screen in custom Application (and not source code redirects)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM