密码保护我的Android应用程序(简单的方法) [英] Password protecting my android app (the simple way)
问题描述
我已经建立了我的第一个应用程序,我想密码保护它。这是为我好存储在Java文件中的密码和方法应当尽可能地简单,因为我对Java的这个程序之前没有任何经验,甚至XML。我也曾有过一些尝试,所以我一直希望有人失败可以帮助我。
我创建的布局与一个EditText字段:
<的EditText
机器人:ID =@ + ID / passwordedittext
机器人:layout_width =200dp
机器人:layout_height =50dp
机器人:inputType =textPassword
机器人:layout_marginTop =40dp
机器人:layout_marginLeft =20dp>
<不是requestFocus />
和一个提交按钮:
<按钮
机器人:ID =@ + ID /提交按钮
机器人:layout_width =50dp
机器人:layout_height =50dp
机器人:layout_marginTop =40dp
机器人:背景=@可绘制/ BGO
机器人:可点击=真
机器人:layout_gravity =右| center_horizontal
机器人:layout_marginRight =20dp/>
Java的文件:
包com.berry;
进口android.app.Activity;
进口android.content.Intent;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;
进口android.widget.Button;
进口android.widget.EditText;
公共类密码延伸活动{
MediaPlayer的mpbuttonclick;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
的setContentView(R.layout.password);
mpbuttonclick = MediaPlayer.create(这一点,R.raw.key preSS);
按钮sumbitButton =(按钮)findViewById(R.id.submitbutton);
sumbitButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
EditText上passwordEditText =(EditText上)findViewById(R.id.passwordedittext);
如果(passwordEditText.getText()的toString()==MyPasswordHere){
startActivity(新的意向书(com.berry.intro));
mpbuttonclick.start();
}}});
}}
该部分:
如果(passwordEditText.getText()的toString()==MyPasswordHere)
不正确。它应该是
如果(passwordEditText.getText()的toString()。等于(MyPasswordHere))
在比较原始数据类型(如 INT
,字符
,布尔
),您可以使用 ==
,!=
等
当比较对象(如字符串
,车
等),你需要使用 .equals ()
方法。
I've built my first app, and I would like to password protect it. It's fine for me to store the password in the Java files and the method needs to be as simple as possible because i have no experience of java or even xml before this app. I've had a few attempts and failed so I was hoping someone can help me out.
I've created the layout with an EditText field:
<EditText
android:id="@+id/passwordedittext"
android:layout_width="200dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp">
<requestFocus />
and a submit button:
<Button
android:id="@+id/submitbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:background="@drawable/bgo"
android:clickable="true"
android:layout_gravity="right|center_horizontal"
android:layout_marginRight="20dp"/>
The Java file:
package com.berry;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class password extends Activity{
MediaPlayer mpbuttonclick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.password);
mpbuttonclick = MediaPlayer.create(this, R.raw.keypress);
Button sumbitButton = (Button) findViewById(R.id.submitbutton);
sumbitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
if(passwordEditText.getText().toString()=="MyPasswordHere"){
startActivity(new Intent("com.berry.intro"));
mpbuttonclick.start();
}}});
}}
This part:
if(passwordEditText.getText().toString()=="MyPasswordHere")
is incorrect. It should be
if(passwordEditText.getText().toString().equals("MyPasswordHere"))
When comparing primitive data types (like int
, char
, boolean
) you can use ==
, !=
, etc.
When comparing objects (like String
, Car
, etc) you need to use the .equals()
method.
这篇关于密码保护我的Android应用程序(简单的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!