
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方法,傳字節流進去。傳字節進去,而且自能傳一個,怎么辦?
用字符串+ 處理 那混合了怎么辦?
加兩個特殊字符進去##(不能用正則表達式的字符)。后面再用 分割字符串的方法分開
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開發登陸案例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答