安卓实践2——登陆功能

来源:互联网 发布:mac 安全模式如何进入 编辑:程序博客网 时间:2024/05/22 05:23

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1、创建一个新的项目,命名为phonelogin</span>

2、功能的界面设计如下


这界面采用的是线性布局与相对布局相结合

2.1 界面代码activity_main.xml如下

<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="com.example.login.MainActivity" >   <EditText        android:id="@+id/et_username"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:hint="请输入用户名"       />  <EditText       android:id="@+id/et_password"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:password="true"       android:hint="请输入密码"      />  <RelativeLayout       android:layout_width="match_parent"      android:layout_height="match_parent"       android:layout_marginTop="20sp"      >      <CheckBox           android:id="@+id/cb_ischeck"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="记住用户名密码"          />      <Button           android:layout_alignParentRight="true"          android:onClick="login"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="登陆"          />        </RelativeLayout>  </LinearLayout>


在这里要对新手特别说明几点:

1、父包裹:match_parent  和 fill_parent 使用方法是一样,只是在安卓2.2之后开发了一个新的叫match_parent ,现在一般使用的都是match_parent ,如果开发低于2.2的应用就用 fill_parent ,它会占用整个屏幕的长和宽;

2、内容包裹:warp_content ,仅仅占用当前控件的长和宽;

3、使用线性布局记得增加方向属性:orientation  

4、要把输入框变成密码输入框:要添加 android:password="true"

5、输入框文字提示:hint

6、控件对齐:layout_alignParentRight="true"

7、单位sp和dp是区别:dp一般用于设置文字大小,设置其他大小用sp;


3.代码MainActivity.java

package com.example.login;import java.util.Map;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends ActionBarActivity {    private EditText et_name;private EditText et_password;private CheckBox cb_ischeck;@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                et_name = (EditText) findViewById(R.id.et_username);        et_password = (EditText) findViewById(R.id.et_password);        cb_ischeck = (CheckBox) findViewById(R.id.cb_ischeck);        Map<String,String>maps = UersInfoUtils.readInfo();        if(maps!=null){        String username = maps.get("username");        String password = maps.get("password");        et_name.setText(username);        et_password.setText(password);        }    } public void login(View v){ String username = et_name.getText().toString().trim(); String password = et_password.getText().toString().trim(); if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){ Toast.makeText(MainActivity.this, "用户名密码不能为空", 1).show(); } else{ System.out.println("链接服务器,进行登陆"); if(cb_ischeck.isChecked()){  boolean result =  UersInfoUtils.savaInfo(username, password); if(result){ Toast.makeText(MainActivity.this, "保存成功", 1).show(); } else{ Toast.makeText(MainActivity.this, "保存失败", 1).show(); } } else{ Toast.makeText(MainActivity.this, "请勾选cb", 1).show(); } }  }   }


4.代码UersInfoUtils.java

package com.example.login;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;public class UersInfoUtils {public static boolean savaInfo(String username,String password){try {String result = username + "##" + password;File file = new File("/data/data/com.example.login/info.txt");FileOutputStream fos = new FileOutputStream(file);fos.write(result.getBytes());fos.close();return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}public static Map<String,String>readInfo(){try {Map<String,String>maps = new HashMap<String,String>();File file = new File("/data/data/com.example.login/info.txt");FileInputStream fis = new FileInputStream(file);BufferedReader bufr =new BufferedReader(new InputStreamReader(fis));String content = bufr.readLine();String[] splits = content.split("##");String username = splits[0];String password = splits[1];maps.put("username",username);maps.put("password", password);fis.close();return maps;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}}




0 0
原创粉丝点击