欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之完成登陆界面的数据保存回显操作实例

程序员文章站 2023-12-19 15:00:04
本文实例讲述了android开发之完成登陆界面的数据保存回显操作。分享给大家供大家参考,具体如下: loginactivity.java: package co...

本文实例讲述了android开发之完成登陆界面的数据保存回显操作。分享给大家供大家参考,具体如下:

loginactivity.java:

package com.example.login; 
import java.util.map; 
import android.app.activity; 
import android.os.bundle; 
import android.text.textutils; 
import android.view.menu; 
import android.view.view; 
import android.widget.button; 
import android.widget.checkbox; 
import android.widget.edittext; 
import android.widget.toast; 
import com.example.login.service.fileservice; 
public class loginactivity extends activity { 
  public edittext edit_name,edit_pass; 
  public button btn_login; 
  public checkbox box_remeber; 
  public fileservice fileservice; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_login); 
    fileservice=new fileservice(this); 
    edit_name=(edittext) findviewbyid(r.id.edit_name); 
    edit_pass=(edittext) findviewbyid(r.id.edit_pass); 
    btn_login=(button) findviewbyid(r.id.btn_login); 
    box_remeber=(checkbox) findviewbyid(r.id.cbx_remember); 
    btn_login.setonclicklistener(new myonclicklistener()); 
    map<string, string> map=fileservice.readfile("private.txt"); 
    if(map!=null){ 
      edit_name.settext(map.get("name")); 
      edit_pass.settext(map.get("pass")); 
    } 
  } 
  @override 
  public boolean oncreateoptionsmenu(menu menu) { 
    // inflate the menu; this adds items to the action bar if it is present. 
    getmenuinflater().inflate(r.menu.login, menu); 
    return true; 
  } 
  class myonclicklistener implements view.onclicklistener{ 
    @override 
    public void onclick(view v) { 
      int id=v.getid(); 
      switch (id) { 
      case r.id.btn_login: 
        string name=edit_name.gettext().tostring(); 
        string pass=edit_pass.gettext().tostring(); 
        if(textutils.isempty(name)){ 
          toast.maketext(loginactivity.this, "用户名不能为空", toast.length_short).show(); 
          return; 
        }else if(textutils.isempty(pass)){ 
          toast.maketext(loginactivity.this, "密码不能为空", toast.length_short).show(); 
          return; 
        }else{ 
          if(box_remeber.ischecked()){ 
            loginactivity.this.fileservice.savetorom(name, pass, "private.txt"); 
            toast.maketext(loginactivity.this, "用户名和密码已保存", toast.length_short).show(); 
          }else{ 
            toast.maketext(loginactivity.this, "用户名和密码不需要保存", toast.length_short).show(); 
          } 
        } 
        break; 
      default: 
        break; 
      } 
      /*if(id==btn_login.getid()){ 
        string name=edit_name.gettext().tostring(); 
        string pass=edit_pass.gettext().tostring(); 
        if(textutils.isempty(name)){ 
          toast.maketext(loginactivity.this, "用户名不能为空", toast.length_short).show(); 
          return; 
        }else if(textutils.isempty(pass)){ 
          toast.maketext(loginactivity.this, "密码不能为空", toast.length_short).show(); 
          return; 
        }else{ 
          if(box_remeber.ischecked()){ 
            loginactivity.this.fileservice.savetorom(name, pass, "private.txt"); 
            toast.maketext(loginactivity.this, "用户名和密码已保存", toast.length_short).show(); 
          }else{ 
            toast.maketext(loginactivity.this, "用户名和密码不需要保存", toast.length_short).show(); 
          } 
        } 
      }*/ 
    } 
  } 
}

fileservice.java:

package com.example.login.service; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.util.hashmap; 
import java.util.map; 
import com.example.login.utils.streamtools; 
import android.content.context; 
public class fileservice { 
  public context context; 
  public fileservice(context context) { 
    this.context = context; 
  } 
  public boolean savetorom(string name,string pass,string filename){ 
    try{ 
      fileoutputstream fos=context.openfileoutput(filename, context.mode_private); 
      string result=name+":"+pass; 
      fos.write(result.getbytes()); 
      fos.flush(); 
      fos.close(); 
    }catch(exception e){ 
      e.printstacktrace(); 
      return false; 
    } 
    return true; 
  } 
  public map<string,string> readfile(string filename){ 
    map<string,string> map=null; 
    try{ 
      fileinputstream fis=context.openfileinput(filename); 
      string value=streamtools.getvalue(fis); 
      string values[]=value.split(":"); 
      if(values.length>0){ 
        map=new hashmap<string, string>(); 
        map.put("name", values[0]); 
        map.put("pass", values[1]); 
      } 
    }catch(exception e){ 
      e.printstacktrace(); 
    } 
    return map; 
  } 
}

streamtools.java:

package com.example.login.utils; 
import java.io.bytearrayoutputstream; 
import java.io.fileinputstream; 
public class streamtools { 
  public static string getvalue(fileinputstream fis) throws exception{ 
    bytearrayoutputstream stream=new bytearrayoutputstream(); 
    byte[] buffer=new byte[1024]; 
    int length=-1; 
    while((length=fis.read(buffer))!=-1){ 
      stream.write(buffer,0,length); 
    } 
    stream.flush(); 
    stream.close(); 
    string value=stream.tostring(); 
    return value; 
  } 
}

login_activity.xml:

<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" 
  android:paddingleft="@dimen/activity_horizontal_margin" 
  android:paddingright="@dimen/activity_horizontal_margin" 
  android:paddingtop="@dimen/activity_vertical_margin" 
  tools:context=".loginactivity" > 
  <linearlayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignparentleft="true" 
    android:layout_alignparenttop="true" 
    android:orientation="vertical" > 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
      <textview 
        android:id="@+id/view_name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/text_name" /> 
      <edittext 
        android:id="@+id/edit_name" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="10"  
        android:inputtype="textpersonname"> 
        <requestfocus /> 
      </edittext> 
    </linearlayout> 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
      <textview 
        android:id="@+id/view_pass" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/text_pass" /> 
      <edittext 
        android:id="@+id/edit_pass" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="10" 
        android:inputtype="textpassword" /> 
    </linearlayout> 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
      <button 
        android:id="@+id/btn_login" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.17" 
        android:text="@string/text_login" /> 
      <checkbox 
        android:id="@+id/cbx_remember" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginleft="80dp" 
        android:text="@string/text_rember" /> 
    </linearlayout> 
  </linearlayout> 
</relativelayout>

string.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="app_name">login</string>
<stringname="action_settings">settings</string>
<stringname="hello_world">login</string>
<stringname="text_name">用户名:</string>
<stringname="text_pass">密 码:</string>
<stringname="text_login">登陆</string>
<stringname="text_rember">记住密码</string>
</resources>

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: