Android App Lock

时间:2016-03-25 14:47:05

标签: android lockscreen

我正在尝试为我的应用应用锁定屏幕。

我认为如果密码不正确,则进入第A页,如果正确,请转到第B页。

我写的是

int PIN = R.id.Txt_password;
int pass = 4444;
if (PIN == pass) {


    //Selecting the button which is to be pressed
    Button Btn_Submit = (Button) findViewById(R.id.Btn_Submit);
    //Creates a listener for the button to react when it is pressed
    Btn_Submit.setOnClickListener(new View.OnClickListener() {
        //Gives the button instructions when it is pressed
        @Override
        public void onClick(View view) {
            Intent startIntent = new Intent(getApplicationContext(),
                    Control_Screen.class);
            startActivity(startIntent);

        }
    });
} else {
    //Selecting the button which is to be pressed
    Button Btn_Submit = (Button) findViewById(R.id.Btn_Submit);
    //Creates a listener for the button to react when it is pressed
    Btn_Submit.setOnClickListener(new View.OnClickListener() {
        //Gives the button instructions when it is pressed
        @Override
        public void onClick(View view) {
            Intent startIntent = new Intent(getApplicationContext(),
                    Password.class);
            startActivity(startIntent);
        }
    });
}

失败了。

这是Txt_password的代码

 <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:ems="10"
        android:id="@+id/Txt_password"
        android:layout_below="@+id/Lbl_EnterPassword"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="4444" />

我认为密码是4444,它是硬编码的,但我觉得情况并非如此,而且存在问题。

我该如何做到这一点?

也许有办法将用户输入密码字段的文本放入字符串中?

4 个答案:

答案 0 :(得分:0)

也许你打算这样做?

EditText txtPassword = (EditText) findViewById(R.id.Txt_password);
int PIN = Integer.parseInt(txtPassword.getText().toString());

if (PIN == 4444) { }

R.id.Txt_password永远不会等于4444

答案 1 :(得分:0)

您没有抓取EditText的文字。你可以这样做:

EditText passwordText = (EditText) findViewById(R.id.Txt_password);
int PASS = Integer.parseInt(passwordText.getText().toString());

现在进行比较。

int pass = 4444;

if(pass == PASS) {
    ...
    ...
}

但是,您需要在按钮的onClickListener内执行此操作。请参阅以下参考代码:

Button Btn_Submit = (Button) findViewById(R.id.Btn_Submit);

    //Creates a listener for the button to react when it is pressed
    Btn_Submit.setOnClickListener(new View.OnClickListener() {
        //Gives the button instructions when it is pressed
        @Override
        public void onClick(View view) {
            EditText passwordText = (EditText) findViewById(R.id.Txt_password);
            int pass = Integer.parseInt(passwordText.getText().toString());

            if(pass == 4444) {
                Intent startIntent = new Intent(getApplicationContext(), Control_Screen.class);
                startActivity(startIntent);
            }
            else {
                Intent startIntent = new Intent(getApplicationContext(), Password.class);
                startActivity(startIntent);
    });

答案 2 :(得分:0)

您需要从EditText字段中获取文本并将其更改为整数值,如下所示:

EditText passText = (EditText) findViewById(R.id.Txt_password);
int PIN = Integer.valueOf(passText.getText().toString());

if (PIN == 4444) { 
    ...
}

答案 3 :(得分:0)

我认为你最好不要走另一条路。不要更改整个按钮侦听器,也不要在方法调用中创建匿名对象。如何将绑定与实际业务逻辑分开?考虑一下:

public class YourFragment extends Fragment implements View.OnClickListener {

    private Button Btn_Submit;
    private EditText Txt_password;

    private final PIN = 4444;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // inflate layout of fragment or set content if this is an activity
        (...)

        Btn_Submit = (Button) findViewById(R.id.Btn_Submit);
        Btn_Submit.setOnClickListener(this);

        Txt_password = (EditText) findViewById(R.id.Txt_password);
    }

    @Override
    public void onClick(View v) {
        // if you have multiple elements with click listeners in this fragment/activity, separate calls here. I'm assuming we get only the submit button here.

        Integer input_pin = Integer.parseInt(Txt_password.getText());
        if (PIN == input_pin) {
            Intent startIntent = new Intent(getApplicationContext(), Control_Screen.class);
            startActivity(startIntent);
        }
        // if pin wrong, reset text field and optionally show the user a notification
        Txt_password.setText("");
    }
}

如果你想让它更干净,可以将onClick中的整个代码放入一个名为onSubmitPin()的方法中,并在按下提交按钮时从onClick调用它。