package com.lidaochen.phonecall;

 import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.ThemedSpinnerAdapter;
import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity{
private EditText et_userName;
private EditText et_pwd;
private CheckBox cb;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化sp实例
sp = getSharedPreferences("test", 0); // 1.获取控件
et_userName = (EditText)findViewById(R.id.et_username);
et_pwd = (EditText)findViewById(R.id.et_pwd);
cb = (CheckBox)findViewById(R.id.cb);
// 把我们需要的数据在test.xml中显示出来,然后在控件上显示出来
String name = sp.getString("name", "");
String pwd = sp.getString("pwd", "");
// 将数据显示在控件上
et_userName.setText(name);
et_pwd.setText(pwd);
}
// 按钮点击事件
public void login(View v)
{
// 获取userName和pwd
String name = et_userName.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
// 判断name和pwd是否为空
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd))
{
Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
}
else
{
System.out.println("登录之后要干的事情!");
if(cb.isChecked())
{
// 获取sp的编辑器
SharedPreferences.Editor edit = sp.edit();
edit.putString("name", name);
edit.putString("pwd", pwd);
// 把edit进行提交
edit.commit();
Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "请勾选复选框", Toast.LENGTH_LONG).show();
}
}
}
}
05-28 23:36