您好,登錄后才能下訂單哦!
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(); } //登錄成功 } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。