Android開發(fā)登陸案例

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;
@Override
protected 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.設(shè)置按鈕的點(diǎn)擊事件
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);//設(shè)置用戶名
et_password.setText(password);
cb_rem.setChecked(true);//設(shè)置復(fù)選框選中狀態(tài)
}
}
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.判斷用戶名密碼是否為空,不為空請(qǐng)求服務(wù)器(省略,默認(rèn)請(qǐng)求成功)
if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
Toast.makeText(mContext, "用戶名密碼不能為空", Toast.LENGTH_SHORT).show();
return ;
}
//請(qǐng)求服務(wù)器,后面講。。。。。。。。。。
//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();
}
}
@Override
public 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;//封裝用戶名密碼
//得到私有目錄下一個(gè)文件寫入流; 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對(duì)象獲取一個(gè)私有目錄的文件讀取流
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對(duì)象獲取私有目錄的一個(gè)路徑
String path = context.getFilesDir().getPath();
System.out.println("...............:"+path);
File file = new File(path,"userinfo.txt");//創(chuàng)建file
FileOutputStream fileOutputStream = new FileOutputStream(file);//創(chuàng)建文件寫入流
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對(duì)象獲取私有目錄的一個(gè)路徑
String path = context.getFilesDir().getPath();
System.out.println("...............:"+path);
File file = new File(path,"userinfo.txt");//創(chuàng)建file
FileInputStream 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------補(bǔ)全抽象方法*/
/*獲取全局變量*****怎么做 fied*/
/*如何格式化代碼*/
/*點(diǎn)擊事件用全局語句*/
/*很多程序都用到contest,所以在類中定義對(duì)象吧!賦值this,以后toast用到直接調(diào)用*/
/*ctrl+1 封裝臨時(shí)自己打的類,crate class*/
/*創(chuàng)建方法*/
/*保存文件*/
1.保存到私有目錄下
2.保存路徑
3.創(chuàng)建一個(gè)File對(duì)象
4.再創(chuàng)建一個(gè)FileOotputStream對(duì)象,傳File進(jìn)去
/*私開包,斯開方法*/
/*保存文件*/
1.保存到私有目錄下 /date/date/包名/
2.保存路徑(特殊)
3.創(chuàng)建一個(gè)File對(duì)象(路徑,文件類型加名字)
4.再創(chuàng)建一個(gè)FileOotputStream對(duì)象,傳File進(jìn)去,其實(shí)就是創(chuàng)建文件寫入流
5.對(duì)讀取這一塊直接 try一下 catch輸出信息(什么stake)
6.FS調(diào)用write方法,傳字節(jié)流進(jìn)去。傳字節(jié)進(jìn)去,而且自能傳一個(gè),怎么辦?
用字符串+ 處理 那混合了怎么辦?
加兩個(gè)特殊字符進(jìn)去##(不能用正則表達(dá)式的字符)。后面再用 分割字符串的方法分開
7.字符串調(diào)用自身 getbyte()方法
8.把流關(guān)閉 FS調(diào)用close()方法
9.最后return ture 告訴保存成功
/*Map?*/
/*toast*/
1.Toast.makeText(mtext,"Stri ng",Toast.選時(shí)間).show
2.mcontext=this ,就是創(chuàng)建一個(gè)數(shù)據(jù)
/*什么時(shí)候回顯*/
1.程序一加載就回顯示
2.是不是要讀取文件才能讀取
3.讀的路徑一樣,創(chuàng)建的文件一樣
4.創(chuàng)建一個(gè)輸入字節(jié)流 FIS(F)
4.用戶名,密碼都是一行,怎么讀取一行
創(chuàng)建BR對(duì)象(new IR(F))
5.創(chuàng)建字符串讀取字節(jié) BR。RL()
6.分割字符串的使用
7.集合的使用 哈希表
8.關(guān)閉流
以上所述是小編給大家介紹的Android開發(fā)登陸案例,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android 使用幀動(dòng)畫內(nèi)存溢出解決方案
這篇文章主要介紹了Android 使用幀動(dòng)畫內(nèi)存溢出解決方案的相關(guān)資料,這里提供了詳細(xì)的解決辦法,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設(shè)計(jì)詳解及實(shí)例
這篇文章主要介紹了房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設(shè)計(jì)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
Kotlin標(biāo)準(zhǔn)庫函數(shù)使用分析及介紹
Kotlin提供了一個(gè)系統(tǒng)庫,是Java庫的增強(qiáng)。其中有很多函數(shù)在適配了Java的類型和方法同時(shí)使用Kotlin的語法。其中一些底層的函數(shù) 是使用比較廣泛的2022-09-09
代碼從windows下visual studio到andriod平臺(tái)遷移實(shí)現(xiàn)步驟
這篇文章主要介紹了代碼從windows下visual studio到andriod平臺(tái)遷移的修改記錄的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android?手寫RecyclerView實(shí)現(xiàn)列表加載
這篇文章主要介紹了Android?手寫RecyclerView實(shí)現(xiàn)列表加載,涉及到列表的需求,肯定第一時(shí)間想到RecyclerView,即便是自定義View,那么RecyclerView也會(huì)是首選,為什么會(huì)選擇RecyclerView而不是ListView,主要就是RecyclerView的內(nèi)存復(fù)用機(jī)制,這也是RecyclerView的核心?2022-08-08
Android 自定義輸入支付密碼的軟鍵盤實(shí)例代碼
這篇文章主要介紹了Android 自定義輸入支付密碼的軟鍵盤實(shí)例代碼的相關(guān)資料,并附簡單實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Android 使用<layer-list>實(shí)現(xiàn)微信聊天輸入框功能
<layer-list> 標(biāo)簽可以設(shè)置LayerDrawable,一種有層次的Drawable疊加效果,<layer-list> 可以包含多個(gè) <item>標(biāo)簽。這篇文章主要介紹了Android 使用<layer-list>實(shí)現(xiàn)微信聊天輸入框,需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)可使用自定義透明Dialog樣式的Activity完整實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)可使用自定義透明Dialog樣式的Activity,結(jié)合完整實(shí)例形式分析了Android Activity自定義style的操作步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07

