layout
<?xml version="1.0"?>-<LinearLayout android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><EditText android:id="@+id/et_username" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="@string/input_username"/><EditText android:id="@+id/et_password" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="@string/input_password" android:inputType="textPassword" android:layout_marginBottom="10dp" android:layout_marginTop="10dp"/>-<RelativeLayout android:layout_height="wrap_content" android:layout_width="fill_parent"><CheckBox android:id="@+id/cb_rem" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/rem_password" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/><Button android:id="@+id/bt_login" android:paddingRight="50dp" android:paddingLeft="50dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/login" android:layout_centerVertical="true" android:layout_alignParentRight="true"/></RelativeLayout></LinearLayout>
java代碼
package com.itheima.login;import java.util.Map;import com.itheima.login.util.UserInfoUtil;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.text.TextUtils;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;public class MainActivity extends Activity implements OnClickListener{private EditText et_username;private EditText et_password;private CheckBox cb_rem;private Button bt_login;private Context mContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = this;et_username = (EditText) findViewById(R.id.et_username);et_password = (EditText) findViewById(R.id.et_password);cb_rem = (CheckBox) findViewById(R.id.cb_rem);bt_login = (Button) findViewById(R.id.bt_login);//b.設定按鈕的點擊事件bt_login.setOnClickListener(this);//f.回顯使用者名稱密碼 ??Map<String, String> map = UserInfoUtil.getUserInfo_android(mContext);//擷取使用者名稱密碼if(map != null){String username = map.get("username");String password = map.get("password");et_username.setText(username);//設定使用者名稱et_password.setText(password);cb_rem.setChecked(true);//設定複選框選中狀態}}private void login(){//c.在onclick方法中,擷取使用者輸入的使用者名稱密碼和是否記住密碼String username = et_username.getText().toString().trim();String password = et_password.getText().toString().trim();boolean isrem = cb_rem.isChecked();//d.判斷使用者名稱密碼是否為空白,不為空白請求伺服器(省略,預設請求成功)if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){Toast.makeText(mContext, "使用者名稱密碼不可為空", Toast.LENGTH_SHORT).show();return ;}//請求伺服器,後面講。。。。。。。。。。//e.判斷是否記住密碼,如果記住,將使用者名稱密碼儲存本地。???? if(isrem){boolean result = UserInfoUtil.saveUserInfo_android(mContext,username,password);if(result){Toast.makeText(mContext, "使用者名稱密碼儲存成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(mContext, "使用者名稱密碼儲存失敗", Toast.LENGTH_SHORT).show(); }}else{Toast.makeText(mContext, "無需儲存", Toast.LENGTH_SHORT).show();}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_login:login();break;default:break;}}}
建立包的代碼
package com.itheima.login.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.content.Context;public class UserInfoUtil {//儲存使用者名稱密碼public static boolean saveUserInfo_android(Context context,String username, String password) {try{String userinfo = username + "##"+ password;//封裝使用者名稱密碼//得到私人目錄下一個檔案寫入流; name : 私人目錄檔案的名稱 mode: 檔案的操作模式, 私人,追加,全域讀,全域寫FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);fileOutputStream.write(userinfo.getBytes());//將使用者名稱密碼寫入檔案fileOutputStream.close();return true;}catch (Exception e) {e.printStackTrace();}return false;}//擷取使用者名稱密碼public static Map<String ,String> getUserInfo_android(Context context){try{//通過context對象擷取一個私人目錄的檔案讀取流FileInputStream fileInputStream = context.openFileInput("userinfo.txt");BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));//讀取一行中包含使用者密碼,需要解析String readLine = bufferedReader.readLine();String[] split = readLine.split("##");HashMap<String, String> hashMap = new HashMap<String ,String>();hashMap.put("username", split[0]);hashMap.put("password", split[1]);bufferedReader.close();fileInputStream.close();return hashMap;}catch (Exception e) {e.printStackTrace();}return null;}//儲存使用者名稱密碼public static boolean saveUserInfo(Context context,String username, String password) {try{String userinfo = username + "##"+ password;//封裝使用者名稱密碼// String path = "/data/data/com.itheima.login/";//指定儲存的路徑//通過Context對象擷取私人目錄的一個路徑String path = context.getFilesDir().getPath();System.out.println("...............:"+path);File file = new File(path,"userinfo.txt");//建立fileFileOutputStream fileOutputStream = new FileOutputStream(file);//建立檔案寫入流fileOutputStream.write(userinfo.getBytes());//將使用者名稱密碼寫入檔案fileOutputStream.close();return true;}catch (Exception e) {e.printStackTrace();}return false;}//擷取使用者名稱密碼public static Map<String ,String> getUserInfo(Context context){try{// String path = "/data/data/com.itheima.login/";//指定儲存的路徑//通過Context對象擷取私人目錄的一個路徑String path = context.getFilesDir().getPath();System.out.println("...............:"+path);File file = new File(path,"userinfo.txt");//建立fileFileInputStream fileInputStream = new FileInputStream(file);BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));//讀取一行中包含使用者密碼,需要解析String readLine = bufferedReader.readLine();String[] split = readLine.split("##");HashMap<String, String> hashMap = new HashMap<String ,String>();hashMap.put("username", split[0]);hashMap.put("password", split[1]);bufferedReader.close();fileInputStream.close();return hashMap;}catch (Exception e) {e.printStackTrace();}return null;}}
我存在的問題與修正
*alt+enter------補全抽象方法*/
/*擷取全域變數*****怎麼做 fied*/
/*如何格式化代碼*/
/*點擊事件用全域語句*/
/*很多程式都用到contest,所以在類中定義對象吧!賦值this,以後toast用到直接調用*/
/*ctrl+1 封裝臨時自己打的類,crate class*/
/*建立方法*/
/*儲存檔案*/
1.儲存到私人目錄下
2.儲存路徑
3.建立一個File對象
4.再建立一個FileOotputStream對象,傳File進去
/*私開包,斯開方法*/
/*儲存檔案*/
1.儲存到私人目錄下 /date/date/包名/
2.儲存路徑(特殊)
3.建立一個File對象(路徑,檔案類型加名字)
4.再建立一個FileOotputStream對象,傳File進去,其實就是建立檔案寫入流
5.對讀取這一塊直接 try一下 catch輸出資訊(什麼stake)
6.FS調用write方法,傳位元組流進去。傳位元組進去,而且自能傳一個,怎麼辦?
用字串+ 處理 那混合了怎麼辦?
加兩個特殊字元進去##(不能用Regex的字元)。後面再用 分割字串的方法分開
7.字串調用自身 getbyte()方法
8.把流關閉 FS調用close()方法
9.最後return ture 告訴儲存成功
/*Map?*/
/*toast*/
1.Toast.makeText(mtext,"Stri ng",Toast.選時間).show
2.mcontext=this ,就是建立一個資料
/*什麼時候回顯*/
1.程式一載入就回顯示
2.是不是要讀取檔案才能讀取
3.讀的路徑一樣,建立的檔案一樣
4.建立一個輸入位元組流 FIS(F)
4.使用者名稱,密碼都是一行,怎麼讀取一行
建立BR對象(new IR(F))
5.建立字串讀取位元組 BR。RL()
6.分割字串的使用
7.集合的使用 雜湊表
8.關閉流
以上所述是小編給大家介紹的Android開發登陸案例,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!