电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

输入密码后自动登录功能的实现


发布日期:2022/7/31
 

今天在用某记账软件我心里就再琢磨着万一被老婆拿到手机胡乱翻一通万一看到了我的用钱流水账那可不好要遭!我要隐私~怎么办呢?于是发现其实人家早已想到为用户考虑到了这个问题有个设置密码功能并且我发现启动后输入密码连以往的登录或者进入进入的按钮都没有省去了只要密码输入完匹配成功就自动进入了好神奇什么样做的呢?这个设计能减少用户输入同时还有心思的设计是就算在没有输入密码的时候也可以进行收入和支出的记录只是看不到详细内容非常好的设计!花了一点时间思考了一下就实现了这个小巧的自动登录功能

下面把代码贴上来吧大虾就勿喷了!

LoginActivityjava

[java]

package comchallen;

import androidappActivity;

import ntentIntent;

import androidosBundle;

import androidtextEditable;

import androidtextTextWatcher;

import androidwidgetEditText;

import androidwidgetTextView;

/**

* Activity which displays a login screen to the user offering registration as

* well

*/

public class LoginActivity extends Activity {

private EditText mPasswordView;

private TextView mLoginStatusMessageView;

private Intent intent;

@Override

protected void onCreate(Bundle savedInstanceState) {

superonCreate(savedInstanceState)

setContentView(Rlayoutmain)

mLoginStatusMessageView = (TextView)findViewById(Ridsign_in_button)

mPasswordView = (EditText) findViewById(Ridpassword)

mPasswordViewaddTextChangedListener(new TextWatcher() {

@Override

public void onTextChanged(CharSequence s int start int before int count) {

if(mPasswordViewgetText()toString()equals()){

intent = new Intent(LoginActivitythissecondActivityclass)

startActivity(intent)

}

}

@Override

public void beforeTextChanged(CharSequence s int start int count int after) {

}

@Override

public void afterTextChanged(Editable s) {

}

})

}

@Override

protected void onResume() {

// 由于我知道你会点击返回键反复试验所以加了这句话

mPasswordViewsetText(

superonResume()

}

}

跳转过去的secondactivity各位就随便弄一个吧!不过需要记住的就是新增了activity后记着要在AndroidManifestxml中添加这个activity

mianxml

[html]

<merge xmlns:android=/apk/res/android

xmlns:tools=/tools

tools:context=LoginActivity >

<! Login form >

<LinearLayout

android:orientation=vertical >

<EditText

android:id=@+id/password

android:layout_width=match_parent

android:layout_height=wrap_content

android:hint=输入密码

android:maxLines=

android:singleLine=true />

<TextView

android:id=@+id/sign_in_button

android:layout_width=wrap_content

android:layout_height=wrap_content

android:layout_gravity=right

android:layout_marginTop=dp

android:paddingLeft=dp

android:paddingRight=dp

android:text=密码匹配成功则自动登录 />

</LinearLayout>

</merge>

其实最关键的就是知道edittext的一些监听的函数了例如还有一些自动补全特别是用于账号输入的时候这里主要就是用到了监听edittext的变化用到了addTextChangedListener并且配合TextWatcher()就能实现在输入的过程中进行一些操作了把登录跳转的方法写在了onTextChanged里

好了我这里预设的密码就是设置死了的大家可以试一试啦!

延伸问题

这款记账软件设置的密码就是保存在本地的那么如何保存读取这个设置的密码呢?(这个其实就设计到了本地数据的存储方法也多种多样后面有时间我们接着讨论这个)

希望和大家多多交流我也是android新兵希望我们在这条路上坚持走下去哪怕是一个小小的功能只要能方便用户就会为我们增加更多的使用者和用户大家加油积少成多集腋成裘!

上一篇:再谈面向对象

下一篇:自定义jtable 实现排序 windows式多选习惯