首先是配置文件:
<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: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" android:orientation="vertical" > <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> <EditText android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="请输入密码" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名和密码" android:layout_centerVertical="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="登录" android:layout_alignParentRight="true" android:onClick="login" /> </RelativeLayout> </LinearLayout>
活动中的代码如下:
package com.itydl.rwinrom; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.InputStreamReader; import org.apache.http.entity.InputStreamEntity; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name; private EditText et_pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_pass = (EditText) findViewById(R.id.et_pass); readAccount();//在onCreate中读取原因是,活动一创建就读取用户名和密码进行回显。 } public void readAccount(){ File file = new File("data/data/com.itydl.rwinrom/info.txt"); if(file.exists()){ try { // FileInputStream fis = new FileInputStream(file); // //把字节流转换成字符流 // BufferedReader br = new BufferedReader(new // InputStreamReader(fis)); BufferedReader br = new BufferedReader(new FileReader(file)); //读取txt文件里的用户名和密码 String text = br.readLine(); String[] s = text.split("##");//正则表达 et_name.setText(s[0]);//ctrl+1提取全局变量 et_pass.setText(s[1]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void login(View v){ String name = et_name.getText().toString(); String pass = et_pass.getText().toString(); CheckBox cb = (CheckBox) findViewById(R.id.cb); //判断选框是否被勾选 if(cb.isChecked()){ //data/data/com.itheima.rwinrom:这就是内部存储空间的路径 File file = new File("data/data/com.itydl.rwinrom/info.txt");//这个路径是安卓特有的文件夹 FileOutputStream fos; try { fos = new FileOutputStream(file); //勾选了复选框,会把用户名密码存入内部存储位置 fos.write((name + "##" + pass).getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //创建并显示吐司对话框 Toast.makeText(this, "登录成功", 0).show(); } }
最后是截图:
当退出程序,再进入时,会发现用户名和密码都回显。
时间: 2024-11-06 03:36:59