码迷,mamicode.com
首页 > 其他好文 > 详细

记住密码

时间:2015-07-06 21:27:17      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 

 

 

 

 

 

技术分享技术分享

activity——main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/b"
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=".MainActivity" >

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/Uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名或手机号"
android:ems="10" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/tvPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/Upass"
android:hint="请输入密码"
android:password="true"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />

<LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent" >

<CheckBox
android:id="@+id/cbremember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:text="记住密码" />

<Button
android:id="@+id/btnlogin"
android:layout_marginLeft="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="登录" />

<Button
android:id="@+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="重置" />

</LinearLayout>

</LinearLayout>

 

 

 

MainActivity:

package com.zwf.rememberpass;

import com.example.rememberpass.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private EditText Uname;
private EditText Upass;
private CheckBox cbremember;
private Button btnlogin;
private Button btnreset;
//保存数据
private SharedPreferences SpSettings =null;
private static final String PREFS_NAME="NamePwd";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
setlistener();
getData();
}


private void setlistener() {
btnreset.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Uname.setText("");
Upass.setText("");

}
});
btnlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if("zwf".equals(Uname.getText().toString())&&"123".equals(Upass.getText().toString())){
//判断复选框是否选中
if(cbremember.isChecked()){
SpSettings=getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
Editor edit=SpSettings.edit();
edit.putBoolean("isKeep", true);
edit.putString("etname", Uname.getText().toString());
edit.putString("etpass", Upass.getText().toString());
edit.commit();
}else{
SpSettings=getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
Editor edit=SpSettings.edit();
edit.putBoolean("isKeep", false);
edit.putString("Uname", "");
edit.putString("Upass", "");
edit.commit();
}
//跳转
Intent intent=new Intent(MainActivity.this,OKActivity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(),"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
}
});
}
private void getData() {
SpSettings=getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
if(SpSettings.getBoolean("isKeep", false)){
Uname.setText(SpSettings.getString("username","" ));
Upass.setText(SpSettings.getString("password", ""));
}else{
Uname.setText("");
Upass.setText("");
}
}


@Override
protected void onResume(){
//在界面显示之前得到之前存储的数据
super.onResume();
getData();
}


private void findView() {
Uname=(EditText) findViewById(R.id.Uname);
Upass=(EditText) findViewById(R.id.Upass);
cbremember=(CheckBox) findViewById(R.id.cbremember);
btnlogin=(Button) findViewById(R.id.btnlogin);
btnreset=(Button) findViewById(R.id.btnreset);
}


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

}

 

记住密码

标签:

原文地址:http://www.cnblogs.com/Z-devotion/p/4625350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!