做一个简单登录界面

来源:互联网 发布:淘宝雪纺短袖衫 编辑:程序博客网 时间:2024/05/18 15:54

先写好登录界面的布局,记住要写一个Checkbox。

<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"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入账号" />    <EditText         android:id="@+id/ed_zhanghao"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入密码" />    <EditText         android:id="@+id/ed_mima"        android:inputType="textPassword"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <RelativeLayout         android:layout_width="match_parent"        android:layout_height="wrap_content">        <CheckBox             android:id="@+id/cb"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="记住密码"/>        <Button             android:onClick="denglu"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:text="登录"/>    </RelativeLayout></LinearLayout>
界面格式如下图:


再写一个保存账号密码的工具类,保存路劲可以写在手机或者SD卡里:

package com.xh.tx.denglu.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.os.Environment;public class Servicein {public static boolean serviceinfo(Context context,String zhanghao,String mima){try {//File f= new File("data/data/com.xh.tx.denglu/info.txt");//context.getFilesDir();帮助我们返回一个目录,data/data/包名/files///context.getCacheDir();这个是缓存目录//Environment.getExternalStorageState();获取SD卡是什么样的状态//String s=Environment.MEDIA_MOUNTED;获取SD卡的插入状态//Environment.getExternalStorageDirectory();返回一个SD卡的路劲;File f= new File(context.getFilesDir(),"info.txt");FileOutputStream fs=new FileOutputStream(f);fs.write((zhanghao+"#####"+mima).getBytes());fs.close();return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}public static Map<String,String> getinfo(Context context){File f= new File(context.getFilesDir(),"info.txt");try {BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(f)));String s=br.readLine();String[] srinfo=s.split("#####");Map<String,String> map=new HashMap<String, String>();map.put("zhanghao",srinfo[0]);map.put("mima",srinfo[1]);return map;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}}
最后在写主代码实现登录功能:

package com.xh.tx.denglu;import java.util.Map;import com.xh.tx.denglu.service.Servicein;import android.os.Bundle;import android.app.Activity;import android.app.Service;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText ed_zhanghao; private EditText ed_mima; private CheckBox cb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed_zhanghao = (EditText) findViewById(R.id.ed_zhanghao);ed_mima = (EditText) findViewById(R.id.ed_mima);cb = (CheckBox) findViewById(R.id.cb);//检查是否有账号密码,如果有,需要回显Map<String, String> map=Servicein.getinfo(this);if(map!=null){ed_zhanghao.setText(map.get("zhanghao"));ed_mima.setText(map.get("mima"));}}public void denglu(View v){String zhanghao = ed_zhanghao.getText().toString().trim();String mima = ed_mima.getText().toString().trim();if(TextUtils.isEmpty(zhanghao)||TextUtils.isEmpty(mima)){Toast.makeText(this, "账号或者密码不能为空", Toast.LENGTH_SHORT).show();}else{if(cb.isChecked()){boolean result=Servicein.serviceinfo(this,zhanghao,mima);if(result){Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();}}if("zhangsan".equals(zhanghao)&&"123456".equals(mima)){Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();}}}}
这样就可以实现一个简单的登录界面了


0 0
原创粉丝点击