做的一个简易QQ登陆界面,能够将账号及密码保存到手机中,并且再次启动该程序的时候会自动显示上次所保存下的账号及密码。
activityMain.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" 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="com.example.qqlogin.MainActivity" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/number" android:hint="QQ号/手机号/邮箱" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/psw" android:inputType="textPassword" android:hint="密码" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:checked="true" android:id="@+id/remember" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/login" android:text="登陆" /> </LinearLayout>
MainActivity.java
package com.example.qqlogin; import java.util.Map; import com.example.qqlogin.utils.utils; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.MenuItem; 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; /** * * @author Caesar * */ public class MainActivity extends Activity implements OnClickListener { private EditText numberText; private EditText pswText; private Button button; private CheckBox cb; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); numberText = (EditText) findViewById(R.id.number); pswText = (EditText) findViewById(R.id.psw); cb = (CheckBox) findViewById(R.id.remember); button = (Button) findViewById(R.id.login); button.setOnClickListener(this); //回显数据 Map<String, String> userInfoMap = utils.GetUserInfo(this); if (userInfoMap != null){ numberText.setText(userInfoMap.get("number")); pswText.setText(userInfoMap.get("psw")); } } public void onClick(View v) { String number = numberText.getText().toString(); String psw = pswText.getText().toString(); String Info = "账号" + number + "密码" + psw; Toast.makeText(this, Info, 0).show(); if (TextUtils.isEmpty(number) || TextUtils.isEmpty(psw)) { Toast.makeText(this, "请正确填写", 0).show(); } if (cb.isChecked()) { if (utils.SaveUserInfo(this,number, psw)) Toast.makeText(this, "保存成功", 0).show(); else { Toast.makeText(this,"保存失败", 0).show(); } } Toast.makeText(this, "登陆成功", 1).show(); } }
utils.java
package com.example.qqlogin.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import android.*; import android.content.Context; import android.text.TextUtils; import android.widget.Toast; /** * 手机内存 文件读取 * @author Caesar * */ public class utils { /** * 保存文件到手机内存 * @param context * @param number * @param psw * @return */ public static boolean SaveUserInfo(Context context, String number, String psw) { try { File fileDir = context.getFilesDir(); File f = new File(fileDir, "data.txt"); FileOutputStream fos; fos = new FileOutputStream(f); String data = number + "##" + psw; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } /** * 从手机内存读取 * @param context * @return */ public static Map<String, String> GetUserInfo(Context context) { try { File fileDir = context.getFilesDir(); File f = new File(fileDir, "data.txt"); FileInputStream fis; fis = new FileInputStream(f); BufferedReader reader = new BufferedReader(new InputStreamReader( fis)); String text = reader.readLine(); if (!text.isEmpty()) { String split[] = text.split("##"); Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", split[0]); userInfoMap.put("psw", split[1]); return userInfoMap; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
时间: 2024-10-12 00:30:21