android 入门 005(登录记住)

android 入门 005(登录记住)

 

<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"
    
     >

   <EditText 
       android:id="@+id/et_name"
            
       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:inputType="textPassword"
       android:hint="请输入密码"
      
       />

   <RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

      

       <CheckBox
           android:id="@+id/cb_rember"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/button1"
           android:layout_alignBottom="@+id/button1"
           android:layout_alignParentLeft="true"
           android:layout_marginLeft="22dp"
           android:text="记住" />
        <Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_gravity="right"
           android:text="登录" 
           android:onClick="login"
           />
   </RelativeLayout>

</LinearLayout>

android 入门 005(登录记住)_第1张图片

2、java 代码

package cn.rfvip.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 android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        remberaccout();
    }

    public void remberaccout() {
        File file = new File("data/data/cn.rfvip.login/info1.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String string_info = br.readLine();
            String[] s = string_info.split("##");
            EditText et_name = (EditText) findViewById(R.id.et_name);
            EditText et_password = (EditText) findViewById(R.id.et_password);
            et_name.setText(s[0]);
            et_password.setText(s[1]);
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

    public void login(View v) {
        EditText et_name = (EditText) findViewById(R.id.et_name);

        EditText et_password = (EditText) findViewById(R.id.et_password);
        String string_et_name = et_name.getText().toString();
        String string_et_password = et_password.getText().toString();

        CheckBox cb_rember = (CheckBox) findViewById(R.id.cb_rember);
        if (cb_rember.isChecked()) {
            File file = new File("data/data/cn.rfvip.login/info1.txt");
            FileOutputStream fos;

            try {
                fos = new FileOutputStream(file);
                fos.write((string_et_name + "##" + string_et_password)
                        .getBytes());
                fos.close();
            } catch (Exception e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }

        }
        Toast.makeText(this, "登录成功!", 0).show();
        Log.i("tag", "登录成功!");
        // Toast.makeText(getApplicationContext(), "登录成功!",
        // Toast.LENGTH_SHORT).show();
        // Toast toast1 = Toast.makeText(getApplicationContext(),
        // "自定义位置Toast", Toast.LENGTH_LONG);
        // toast1.setGravity(Gravity.CENTER, 100, 100);
        // toast1.show();

    }

}

 

效果:自动记住了

android 入门 005(登录记住)_第2张图片

你可能感兴趣的:(android 入门 005(登录记住))