密码保护Android应用程序

时间:2014-03-18 10:33:46

标签: android passwords

我正在开发一个应用程序。我需要密码保护它,即在允许访问主要内容之前需要输入密码。

密码可以本地存储在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

<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>

1 个答案:

答案 0 :(得分:0)

尝试这个

  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();
                }
            }
        });
    }