同比上篇增加
zhuce_layout.xml对button添加了点击事件
<Button
android:id="@+id/btn_tiaozhuang"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="@drawable/baocun"
android:onClick="Start_baocun"
/>
zhuce_Activity.java增加了控件ID的获取,数据判断,数据储存等功能,代码整篇copy
package com.example.app_login;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;
public class zhuce_Activity extends Activity {
private SharedPreferences.Editor editor;
EditText ed_zhanghao=null;
EditText ed_mima01=null;
EditText ed_mima02=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 去掉页面的标题栏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.zhuce_layout);
//实例化SharePreferences对象
SharedPreferences sh=this.getSharedPreferences("TT", Activity.MODE_PRIVATE);
//实例化存储对象
editor =sh.edit();
FindView();
}
private void FindView(){
ed_zhanghao=(EditText)findViewById(R.id.ed_zhanghao);
ed_mima01=(EditText)findViewById(R.id.ed_mima01);
ed_mima02=(EditText)findViewById(R.id.ed_mima02);
}
//点击保存按钮的方法
public void Start_baocun(View v)
{
String stg_zhanghao=ed_zhanghao.getText().toString().trim();
String stg_mima01=ed_mima01.getText().toString().trim();
String stg_mima02=ed_mima02.getText().toString().trim();
//如果不一样
if(!stg_mima01.equals(stg_mima02)){
Toast.makeText(this, "密码不一致", 1).show();
ed_mima01.setText("");
ed_mima02.setText("");
return ;
}
editor.putString("zhanghao",stg_zhanghao);
editor.putString("mima",stg_mima01);
editor.commit();//数据提交
Toast.makeText(this, "保存的账号"+stg_zhanghao+",密码"+stg_mima01, 1).show();
//关闭当前界面
this.finish();
}
}
MainActivity.java
增加了实例化SharePreferences对象
同时判断账号是否存在
下列存在于onCreate方法中
//实例化SharePreferences对象
final SharedPreferences sh=this.getSharedPreferences("TT", Activity.MODE_PRIVATE);
下列代码存在于按钮的点击事件监听中
btn_01.setOnClickListener(new OnClickListener(){
})
String stg_zhanghao=sh.getString("zhanghao", "");
String stg_mima=sh.getString("mima","");
// 给定一个默认的账号密码
if (stg_01.equals(stg_zhanghao) && stg_02.equals(stg_mima)) {
Toast.makeText(MainActivity.this, "登录成功", 1).show();
} else {
Toast.makeText(MainActivity.this, "登录失败,清空数据", 1).show();
// 清空输入框内容
ed_01.setText("");
ed_02.setText("");
}