android基础-----登录界面

这一部分主要学习

1、在安卓下将登录界面的信息保存成文件,并将文件读取出来显示在登录界面。

2、toast的使用;

3、hashmap的使用;



MainActivity.java

package com.luoriver.login;

import java.io.IOException;
import java.util.Map;

import javax.security.auth.PrivateCredentialPermission;

import com.luoriver.login.service.loginService;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText et_input_name;
	private EditText et_input_password; 
	private CheckBox cb_remember;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et_input_name = (EditText) findViewById(R.id.et_input_name);
		et_input_password = (EditText) findViewById(R.id.et_input_passwd);
		cb_remember = (CheckBox) findViewById(R.id.cb_rember_passwd);
		
		Map<String, String> map = new loginService().getSaveUserInfo(this);
		
		if(map!=null){
			et_input_name.setText(map.get("username"));
			et_input_password.setText(map.get("password"));
		}
	}
	
	public void login(View view){
		String username = et_input_name.getText().toString().trim();
		String password = et_input_password.getText().toString().trim();
		
		if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
			Toast.makeText(this, "用户名或密码不能为空,请重新输入", Toast.LENGTH_SHORT).show();
			
		}else{
			
			/**
			 * remember password checkbox
			 */
			
			if(cb_remember.isChecked()){
				
				boolean result = loginService.saveUserInfo(this, username, password);
				if(result){
					Toast.makeText(this, "密码已经保存了", Toast.LENGTH_LONG).show();
				}
			}
			
			if("luoriver".equals(username) && "123456".equals(password)){
				Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
				
			}
		}
	}
}






 

loginService.java

package com.luoriver.login.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.renderscript.FieldPacker;

/**
 * 保存用户名,密码的业务方法 
 * @author luoriver
 *@param username 用户名
 *@param password 密码
 *@return true 保存成功 false  保存失败
 * 2014-11-2上午7:20:56
 */
public class loginService {
	public static boolean saveUserInfo(Context context, String username, String password) throws IOException{
		try {
			
			
			File file = new File(context.getFilesDir(), "info.txt");

		//	context.getFilesDir();  //帮助我们返回一个目录 "/data/data/包名/files"
			
			FileOutputStream fos = new FileOutputStream(file);
			
			fos.write((username +"##" + password).getBytes());
			fos.close();
			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * 获取保存的数据
	 * @param context
	 * @return
	 */
	@SuppressWarnings("resource")
	public static Map<String, String> getSaveUserInfo(Context context){
		
		File file = new File(context.getFilesDir(), "info.txt");
		try {
			FileInputStream fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String str = br.readLine();
			
			String[] infos =str.split("##");
			
			Map<String, String> map = new HashMap<String, String>();
			map.put("username", infos[0]);
			map.put("password", infos[1]);
			
			return map;
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<TextView 
	    android:id="@+id/tv_input_name"
		android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入用户名"
	    
	    />
    <EditText
        android:id="@+id/et_input_name"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:ems="10" >

        <requestFocus />
    </EditText>
    
    <TextView 
	    android:id="@+id/tv_input_passwd"
		android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入用户名"
	    
	    />

    <EditText
        android:inputType="textPassword"
        android:id="@+id/et_input_passwd"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        android:ems="10" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.77" >

        <CheckBox
            android:id="@+id/cb_rember_passwd"
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="18dp"
            android:text="记住密码" />

        <Button
            android:onClick = "login"
            android:id="@+id/bt_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/cb_rember_passwd"
            android:layout_alignBottom="@+id/cb_rember_passwd"
            android:layout_alignParentRight="true"
            android:layout_marginRight="60dp"
            android:text="登陆" />
        
    </RelativeLayout>

</LinearLayout>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值