网友通过本文主要向大家介绍了印象笔记扫描宝安卓版,印象笔记安卓版,安卓笔记,印象笔记安卓,安卓笔记软件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
安卓第二天笔记-数据保存,安卓第二天笔记保存
安卓第二天笔记--数据保存
1.保存数据私有文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:background="#0090CA" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/qq"/> <!--号码 --> <EditText android:id="@+id/et_qqNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="请输入qq号码"/> <!--密码 --> <EditText android:id="@+id/et_qqPwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="请输入qq密码"/> <!--记住密码复选框 --> <CheckBox android:id="@+id/cb_remember" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="记住密码"/> <!--登录按键 --> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#09A3DC" android:textSize="20sp" android:textColor="#fff" android:text="登录"/> </LinearLayout>
Activity
/** * QQ 登录 保存密码到私有文件 * 步骤 * 1.获取输入的用户名与密码 * 2.判断是否 为空,为空就给用户提示Toast * 3.保存用户名与密码到文件 * 4.数据回显 * * @author 刘楠 * * 2016-2-18下午12:39:54 */ public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity"; /* * QQ号码 */ private EditText et_qqNumber; /* * QQ密码 */ private EditText et_qqPwd; /* * 记住密码,复选框 */ private CheckBox cb_remember; /* * 登录按键 */ private Button btn_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * 初始化 */ et_qqNumber = (EditText) findViewById(R.id.et_qqNumber); et_qqPwd = (EditText) findViewById(R.id.et_qqPwd); cb_remember = (CheckBox) findViewById(R.id.cb_remember); btn_login = (Button) findViewById(R.id.btn_login); /* * 为登录按键设置单击事件 */ btn_login.setOnClickListener(this); /* * 回显示数据 */ Map<String, String> map=QQLoginUtils.getUser(this); if(map!=null){ et_qqNumber.setText(map.get("username")); et_qqPwd.setText(map.get("password")); String isChecked = map.get("isChecked"); if("true".equals(isChecked)){ cb_remember.setChecked(true); }else{ cb_remember.setChecked(false); } } } /** * 单击事件,监听器 */ @Override public void onClick(View v) { // 判断ID switch (v.getId()) { case R.id.btn_login: // 执行相应的方法 qqLogin(); break; default: break; } } /** * 登录方法 */ public void qqLogin() { /* * 获取用户名与密码 */ String qq = et_qqNumber.getText().toString().trim(); String pwd = et_qqPwd.getText().toString().trim(); // 判断是否为空 if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) { Toast.makeText(this, "亲,qq号码 与密码不能为空!", Toast.LENGTH_SHORT).show(); return; } /* * 判断 是否要保存 */ if (cb_remember.isChecked()) { Log.i(TAG, "保存用户名与密码"); // 要保存 boolean flag = QQLoginUtils.saveUser(this, qq, pwd,cb_remember.isChecked()); // 判断 是否保存成功 if (flag) { Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } } else { //不要保存 Log.i(TAG, "不保存用户名与密码"); boolean flag = QQLoginUtils.saveUser(this, "", "",cb_remember.isChecked()); //