xml文件
<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"
android:padding="5dp"
>
<EditText
android:id="@+id/qqnum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入QQ"
android:inputType="number"
android:textSize="20dp"
/>
<EditText
android:id="@+id/pass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:textSize="20dp"
/>
<CheckBox
android:id="@+id/rem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
/>
<Button
android:id="@+id/Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
Utils
package com.example.android22filelogin;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import android.text.TextUtils;
public class Utils {
public static boolean saveUserInfo(String username,String pwd){
String data=username+"##"+pwd;
String path="/data/data/com.example.android22filelogin/data.txt";
try {
FileOutputStream out=new FileOutputStream(path);
out.write(data.getBytes());
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static Map<String,String> getUserInfo(){
String path="/data/data/com.example.android22filelogin/data.txt";
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String data=reader.readLine();
if(!TextUtils.isEmpty(data))
{
String [] datas=data.split("##");
Map<String, String> userinfo=new HashMap<String, String>();
userinfo.put("number", datas[0]);
userinfo.put("pwd", datas[1]);
return userinfo;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
activity
public class MainActivity extends Activity implements OnClickListener {
private EditText qqnum,pwd;
private CheckBox rem;
private Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
qqnum=(EditText)findViewById(R.id.qqnum);
pwd=(EditText)findViewById(R.id.pass);
rem=(CheckBox)findViewById(R.id.rem);
but=(Button)findViewById(R.id.Login);
but.setOnClickListener(this);
//回显数据
Map<String, String> userinfo=Utils.getUserInfo();
if(userinfo!=null)
{
qqnum.setText(userinfo.get("number"));
pwd.setText(userinfo.get("pwd"));
}
}
@Override
public void onClick(View v) {
//记住号码和密码
String num=qqnum.getText().toString();
String password=pwd.getText().toString();
if(TextUtils.isEmpty(num)||TextUtils.isEmpty(password))
{
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
return;
}
//判断是否记住密码
if(rem.isChecked())
{
boolean isSuccess=Utils.saveUserInfo(num, password);
Toast.makeText(this, isSuccess+"", Toast.LENGTH_LONG).show();
}
//登录成功
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。