账户信息(注册、登录)

public class AccountActivity extends AppCompatActivity {

    private EditText accountName;
    private EditText passWord;
    private Button login;
    private Button register;
    private String inputName;
    private String inputPassword;

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

        accountName=(EditText) findViewById(R.id.accountName);
        passWord=(EditText) findViewById(R.id.password);




        register=(Button) findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputName = accountName.getText().toString();
                inputPassword = passWord.getText().toString();

                FileOutputStream out= null;
                File file = new File("//storage/sdcard1/New Folder/accountInfo.txt");
                 // File file=Environment.getExternalStorageDirectory();
                // Log.d("debug",file.getAbsolutePath());
                //Log.d("debug",file.getCanonicalPath()+);


                BufferedWriter writer=null;
                try{

                    //out=openFileOutput("R.raw.data", Context.MODE_PRIVATE);
                    //writer=new BufferedWriter(new OutputStreamWriter(out));

                    out=new FileOutputStream(file);
                    writer=new BufferedWriter(new OutputStreamWriter(out));

                    if((inputName.equals("")) || (inputPassword.equals("")))
                    {
                        Toast.makeText(AccountActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    }
                    writer.write(inputName);
                    writer.newLine();
                   // writer.write("\r\n");
                    writer.write(inputPassword);
                    Toast.makeText(AccountActivity.this,"注册成功!",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(AccountActivity.this,MainActivity.class);
                    startActivity(intent);


                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try{
                        if(writer!=null){
                            writer.close();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                }
        });
        login=(Button) findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputName = accountName.getText().toString();
                inputPassword = passWord.getText().toString();

                FileInputStream in=null;
               // InputStream in=getResources().openRawResource(R.raw.data);
                File file = new File("//storage/sdcard1/New Folder/accountInfo.txt");
                BufferedReader reader=null;
                String line="";
                try{
                    //in=openFileInput("data");

                    in=new FileInputStream(file);
                    reader=new BufferedReader(new InputStreamReader(in));
                    while((line=reader.readLine())!=null){
                        if(line.equals(inputName)){
                            line=reader.readLine();
                            if(line.equals(inputPassword)){
                                Toast.makeText(AccountActivity.this,"登陆成功!",Toast.LENGTH_SHORT).show();
                                Intent intent=new Intent(AccountActivity.this,MainActivity.class);
                                startActivity(intent);
                            }
                            else
                            {
                                Toast.makeText(AccountActivity.this,"密码错误,请重新输入!",Toast.LENGTH_SHORT).show();
                                passWord.setText("");
                            }
                        }else{
                            Toast.makeText(AccountActivity.this,"该用户不存在,请注册!",Toast.LENGTH_SHORT).show();
                           accountName.setText("");
                            passWord.setText("");
                        }
                    }


                }catch (Exception e){
                    e.printStackTrace();

                }finally {
                    try{
                        if(reader!=null){
                            reader.close();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

            }
        });

    }

    @Override
    protected  void onDestroy(){
        super.onDestroy();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值