Password protecting my android app (the simple way)(密码保护我的 android 应用程序(简单的方法))
问题描述
我已经构建了我的第一个应用程序,我想用密码保护它.我可以将密码存储在 Java 文件中,并且方法需要尽可能简单,因为在此应用程序之前我没有 java 甚至 xml 的经验.我已经尝试过几次但都失败了,所以我希望有人可以帮助我.
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.
我已经创建了带有 EditText 字段的布局:
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 />
还有一个提交按钮:
<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"/>
Java 文件:
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();
}}});
}}
推荐答案
这部分:
if(passwordEditText.getText().toString()=="MyPasswordHere")
不正确.应该是
if(passwordEditText.getText().toString().equals("MyPasswordHere"))
当比较原始数据类型(如int、char、boolean)时,您可以使用==,!= 等
在比较对象时(如 String、Car 等),您需要使用 .equals() 方法.
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 应用程序(简单的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:密码保护我的 android 应用程序(简单的方法)
基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
