简体   繁体   English

密码保护Android应用

[英]Password protect android app

I have an application which I am developing. 我有一个正在开发的应用程序。 I need to password protect it, ie a password needs to be entered before allowing access to the main content. 我需要用密码保护它,即在允许访问主要内容之前需要输入密码。

Password can be stored locally in the Java file. 密码可以本地存储在Java文件中。

LoginActivity.java LoginActivity.java

package com.example.capitacustomerfactsheets;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        final EditText passWord = (EditText) findViewById(R.id.editpassword);
        final Button loginButton = (Button) findViewById(R.id.btn_submit);
        loginButton .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(passWord.getText().toString().equals("123456")) {
                    final Intent myIntent = new Intent();
                    myIntent.setComponent(new ComponentName(LoginActivity.this,MainActivity.class));
                    startActivity(myIntent);
                    finish();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

}

activity_login.xml activity_login.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".LoginActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/password" />

<EditText
    android:id="@+id/editpassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:ems="10"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btn_submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="Login" />

</RelativeLayout>

try this one 试试这个

  public void onCreate(Bundle savedInstanceState)
    { 
        setContentView(R.layout.activity_login);
        final EditText passWord = (EditText) findViewById(R.id.editpassword);
        final Button loginButton = (Button) findViewById(R.id.btn_submit);
        loginButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(passWord.getText().toString().equals("123456")) {
                    final Intent myIntent = new Intent();
                    myIntent.setComponent(new ComponentName(LoginActivity.this,your new activity.class));
                    startActivity(myIntent);
                    finish();
                }
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM